Does anyone know how to get node position using xpath?
Let's say I have the following xml:
<a> <b>zyx</b> <b>wvu</b> <b>tsr</b> <b>qpo</b> </a>
I can use the following xpath request to select the third <b> node (<b> tsr </b>):
a/b[.='tsr']
That all is well and good, but I want to return the ordinal position of this node, something like:
a/b[.='tsr']/position()
(but a little more work!)
Is it possible?
edit : Forgot to mention that I am using .net 2, so it is xpath 1.0!
Update : Finished with the help of James Sulak a great answer . For those interested here, my implementation in C #:
int position = doc.SelectNodes("a/b[.='tsr']/preceding-sibling::b").Count + 1; // Check the node actually exists if (position > 1 || doc.SelectSingleNode("a/b[.='tsr']") != null) { Console.WriteLine("Found at position = {0}", position); }
xpath
Wilfred Knievel Oct 22 '08 at 15:51 2008-10-22 15:51
source share