给定
带有全局变量的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>
在XSLT1.0中,不允许在匹配表达式中使用变量。
来自XSLT1.0规范:定义模板规则
match属性的值包含可变参考。
XSLT 2.0中的匹配表达式中允许使用变量。
来自XSLT2.0规范:模式语法
模式可以从id FO或键函数调用开始,前提是要匹配的值以文字或引用的形式提供变量或参数,以及键名称(在键的情况下函数)作为字符串文字提供。这些模式永远不会匹配树中根不是文档节点的节点。