Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

xslt - Variable in xsl:template matching pattern

Given

An XSLT stylesheet with a global variable:

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

Question

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Variables are not allowed to be used in match expressions in XSLT 1.0.

From the XSLT 1.0 specification: Defining Template Rules

It is an error for the value of the match attribute to contain a VariableReference.

Variables are allowed in match expressions in XSLT 2.0.

From the XSLT 2.0 specification: Syntax of Patterns

Patterns may start with an id FO or key function call, provided that the value to be matched is supplied as either a literal or a reference to a variable or parameter, and the key name (in the case of the key function) is supplied as a string literal. These patterns will never match a node in a tree whose root is not a document node.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...