Consider the following XML
<x n="1">
<x n="2">
<test/>
</x>
</x>
and transform it via the following XSLT:
<xsl:variable name="x" select="ancestor::x"/>
<xsl:value-of select="$x[1]/@n"/>
What is the expected result? Is it "1" or "2"?
It is "1".
I failed to find a definite answer in W3C specs.
The language used to specify this in the W3C XPath 1.0 spec is
notoriously obscure.
Section 2.4 states that a predicate filters a node-set with respect to
an axis.
The construct $x[1] is a FilterExpr, described in section 3.3. This
states "The predicate filters the expression with respect to the child
axis".
The child axis is a forwards axis. Section 2.4 tells you that when a
node-set is filtered with respect to a forwards axis, the proximity
position of each node is its position in the sequence in forwards
document order. $x[1] selects the node whose proximity position is 1,
that is, the one that is first in document order.
The mention of the "child axis" is a complete red herring here, it's
just a device to ensure that $x is filtered in forwards document order.
I have run into this question because the result of
MSXML 3.0 and Saxon differs from that of MSXML 4.0:
the former give "1", while the latter gives "2".
Who is right? Or they are both right?
It looks as if MSXML4 has a bug.
Michael Kay
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list