本文介绍了xsl:template 匹配模式中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定

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

An XSLT stylesheet with a global variable:

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

问题

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

Where from comes the limitation that using variables in predicates is incorrect in the xsl:template matching pattern, but is acceptable in xsl:apply-templates selecting pattern?

<!-- 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>

推荐答案

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

来自 XSLT 1.0 规范:定义模板规则

From the XSLT 1.0 specification: Defining Template Rules

匹配属性的值包含一个错误变量引用.

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

来自 XSLT 2.0 规范:模式语法

From the XSLT 2.0 specification: Syntax of Patterns

模式可以以 id FO 或 key 函数调用开头,前提是要匹配的值作为文字或引用提供到变量或参数,以及键名(在键的情况下函数)作为字符串文字提供.这些模式永远不会匹配树中根不是文档节点的节点.

这篇关于xsl:template 匹配模式中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 06:08
查看更多