Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable in xsl:template matching pattern

Tags:

xslt

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>
like image 688
newtover Avatar asked Jan 12 '12 17:01

newtover


1 Answers

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.

like image 75
Mads Hansen Avatar answered Nov 24 '22 04:11

Mads Hansen