XSLT 变量未被识别。

huangapple go评论70阅读模式
英文:

XSLT variable not recognized

问题

我以前使用过XSLT变量,但出现了一个问题,我无法让样式表看到分配的变量。当我复制示例代码时,它似乎可以工作,所以肯定是我做错了什么。以下是代码。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:template match="/JDF">
        <xsl:variable name="customer" 
            select="/JDF/ResourcePool[1]/CustomerInfo[1]/@CustomerID"/>
        <job>
            <jobInfo>{$customer}</jobInfo>
        </job>
    </xsl:template>
</xsl:stylesheet>

当我运行上面的代码时,结果如下。

<?xml version="1.0" encoding="UTF-8"?>
<job>
   <jobInfo>[此处是您的变量值]</jobInfo>
</job>

<jobInfo>元素中,您应该看到$customer变量的值,而不是字符串"$customer"

英文:

I've worked with XSLT variables before, but for some reason, I can not get the stylesheet to see an assigned variable. When I copy sample code it seems to work, so it has to be something I'm doing wrong. Below is the code.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet
	xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;3.0&quot;&gt;
	&lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot; /&gt;
	&lt;xsl:template match=&quot;/JDF&quot;&gt;
	&lt;xsl:variable name=&quot;customer&quot; 
        select=&quot;/JDF/ResourcePool[1]/CustomerInfo[1]/@CustomerID&quot;/&gt;
			&lt;job&gt;
				&lt;jobInfo&gt;$customer&lt;/jobInfo&gt;
			&lt;/job&gt;
	&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

When I run the above the result is this.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;job&gt;
   &lt;jobInfo&gt;$customer&lt;/jobInfo&gt;
&lt;/job&gt;

答案1

得分: 4

XSLT 1.0, 2.0

Change

<jobInfo>$customer</jobInfo>

to

<jobInfo>
<xsl:value-of select="$customer"/>
</jobInfo>

XSLT 3.0+

If text value templates are enabled (using, for example xsl:stylesheet/@expand-text=&quot;yes&quot;), then you could use:

<jobInfo>{$customer}</jobInfo>

英文:

XSLT 1.0, 2.0

Change

&lt;jobInfo&gt;$customer&lt;/jobInfo&gt;

to

&lt;jobInfo&gt;
  &lt;xsl:value-of select=&quot;$customer&quot;/&gt;
&lt;/jobInfo&gt;

XSLT 3.0+

If text value templates are enabled (using, for example xsl:stylesheet/@expand-text=&quot;yes&quot;), then you could use:

&lt;jobInfo&gt;{$customer}&lt;/jobInfo&gt;

huangapple
  • 本文由 发表于 2023年7月7日 04:33:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定