[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] finding the position of a node within a node set


Subject: Re: [xsl] finding the position of a node within a node set
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Sun, 26 Aug 2012 09:07:22 +0100

On 26/08/2012 07:49, Ihe Onwuka wrote:
Assuming Xpath 2.0 take a look at the index-of function.



No, index-of() is no use because it compares nodes by value rather than by identity.


The question is a bit confusing because you talk of a "node-set" which is XPath 1.0 terminology; but a set of nodes by definition has no particular order and therefore the nodes it contains have no particular position. The code you showed finds the position of a node within the sequence of nodes formed by sorting the nodes in the node-set into document order.

If you are using XPath 2.0, and if what you mean is a node sequence rather than a node set, then you can find the positions of a node within the sequence (the same node might be at more than one position...) using

for $i in 1 to count($nodes)) return $i[$nodes[$i] is $node]

Michael Kay
Saxonica


Current Thread
Keywords