我的 XSLT 变量未加载到模板中
My XSLT Variable doesn't load in template
所以我尝试创建一个变量,然后在我的模板中使用它。
这是我现在的代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="items">
<xsl:call-template name="item" />
</xsl:template>
<xsl:template name="item">
<xsl:for-each select="item">
<xsl:variable name="currentItemTheme">
test variable
</xsl:variable>
<xsl:if test="title = name">
Showvariable: {$currentItemTheme} <br/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我不知道为什么它不起作用。我的文件确实输出了“Showvariable:”,但它只是不显示测试值。所以 for-each 循环和模板不是问题。
有什么我想念的吗?有人知道我如何让它工作吗?
我认为您需要 <xsl:value-of select="$currentItemTheme"/>
而不是 {$currentItemTheme}
,除非您将 XSLT 代码与提供 {$currentItemTheme}
语法的其他语言混合使用。
<xsl:copy-of select="$currentItemTheme"/>
是另一种选择,如果您在变量中构建节点并希望输出它们而不只是将字符串值作为文本节点。
所以我尝试创建一个变量,然后在我的模板中使用它。
这是我现在的代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="items">
<xsl:call-template name="item" />
</xsl:template>
<xsl:template name="item">
<xsl:for-each select="item">
<xsl:variable name="currentItemTheme">
test variable
</xsl:variable>
<xsl:if test="title = name">
Showvariable: {$currentItemTheme} <br/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我不知道为什么它不起作用。我的文件确实输出了“Showvariable:”,但它只是不显示测试值。所以 for-each 循环和模板不是问题。
有什么我想念的吗?有人知道我如何让它工作吗?
我认为您需要 <xsl:value-of select="$currentItemTheme"/>
而不是 {$currentItemTheme}
,除非您将 XSLT 代码与提供 {$currentItemTheme}
语法的其他语言混合使用。
<xsl:copy-of select="$currentItemTheme"/>
是另一种选择,如果您在变量中构建节点并希望输出它们而不只是将字符串值作为文本节点。