XSLT: attribute value used as numeric predicate
Given
<xsl:variable name="datePrecision" as="element()*">
<p>Year</p>
<p>Month</p>
<p>Day</p>
<p>Time</p>
<p>Timestamp</p>
</xsl:variable>
The expression
$datePrecision[5]
returns a nodeSet containing one text node with value "Timestamp", as expected.
Later in a template, with a context element having an attribute
@precision="5"
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[5] 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 9.2.0.3)
Obviously I've missed something fundamental
Yes. The XPath expression:
$datePrecision[@precision]
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:
$datePrecision[current()/@precision]
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]
精彩评论