Find position of a node using xpath -
anyone know how position of node using xpath?
say have following xml:
<a> <b>zyx</b> <b>wvu</b> <b>tsr</b> <b>qpo</b> </a>
i can use following xpath query select third <b> node (<b>tsr</b>):
a/b[.='tsr']
which , want return ordinal position of node, like:
a/b[.='tsr']/position()
(but bit more working!)
is possible?
edit: forgot mention using .net 2 it's xpath 1.0!
update: ended using james sulak's excellent answer. interested here's implementation in c#:
int position = doc.selectnodes("a/b[.='tsr']/preceding-sibling::b").count + 1; // check node exists if (position > 1 || doc.selectsinglenode("a/b[.='tsr']") != null) { console.writeline("found @ position = {0}", position); }
try:
count(a/b[.='tsr']/preceding-sibling::*)+1.
Comments
Post a Comment