XSLT: attribute value used as numeric predicate
<xsl:variable name="datePrecision" as="element()*"> <p>Year</p> <p>Month</p> <p>Day</p> <p>Time</p> <p>Timestamp</p> </xsl:variable>
returns a nodeSet containing one text node with value "Timestamp", as expected.
Later in a template, with a context element having an attribute
I try the following expressions but all return an empty string:
$datePrecision[@precision] $datePrecision[number(@precision)] $datePrecision[xs:decimal(@precision)]
However, the following sequence does what I want
<xsl:variable name="prec" select="number(@precision)"/> ... $datePrecision[$prec] ...
Using Oxygen/XML's debugger I've stepped to the point where the expression is about to be evaluated and display the following in the watch window:
Expression Value Nodes/Values Set -------------------------- --------------- ----------------------- $datePrecision Node Set(1) #text Timestamp @precision Node Set(1) precision 5 $datePrecision[@precision] number(@precision) 5 $datePrecision[number(@precision)] $prec 5 $datePrecision[$prec] Node Set(1) #text Timestamp
Obviously I've missed something fundamental about how attribute nodes are atomized for use in a predicate, but can't find anything in the docs (Michael Kay's XSLT/XPATH 2.0, 4th ed) that would explain this difference.
Can someone explain why this is occurring, and point me to where, in either the XSLT 2.0 spec or Michael Kay's book, where this is described?
(the XSLT processor is Saxon-PE 126.96.36.199)
Obviously I've missed something fundamental
Yes. The XPath expression:
means: all elements in $datePrecision that have an attribute named precision.
But you want @precision to mean the attribute named precision of the currnet node that is matched by the template.
XSLT provides the current() function exactly for this purpose. Use:
UPDATE: As Martin Honnen hinted, the OP probably wants to get the 5th element out of $datePrecision -- something not immediately visible from the description of the problem. In this case, it may be necessary to use:
$datePrecision[position() = current()/@precision]