xsl中的变量:模板匹配模式

时间:2012-01-12 17:00:28

标签: xslt

鉴于

带有全局变量的XSLT样式表:

<xsl:variable name="lang" select="/response/str[@name='lang']"/>

问题

xsl:template匹配模式中,谓词中使用变量的限制是不正确的,但在xsl:apply-templates选择模式中是否可以接受?

<!-- throws compilation error, at least in libxslt --> 
<xsl:template match="list[@name='item_list'][$lang]"/>

<!-- works as expected --> 
<xsl:template match="list[@name='item_list'][/response/str[@name='lang']]"/>

<!-- works as expected --> 
<xsl:template match="response">
    <xsl:apply-templates select="list[@name='item_list'][$lang]">
</xsl:template>

1 个答案:

答案 0 :(得分:11)

不允许在XSLT 1.0中的匹配表达式中使用变量。

来自XSLT 1.0规范:Defining Template Rules

  

match属性的值包含a是错误的   VariableReference。

XSLT 2.0中的匹配表达式中允许使用变量

来自XSLT 2.0规范:Syntax of Patterns

  

模式可以以id FO或键函数调用开始,前提是   要匹配的值以文字或引用的形式提供   变量或参数,以及密钥名称(在密钥的情况下)   function)以字符串文字形式提供。这些模式永远不会   匹配树的节点,该树的根不是文档节点。

相关问题