Find position of a node within a nodeset using xpath
- by Phil Nash
After playing around with position() in vain I was googling around for a solution and arrived at this older stackoverflow question which almost describes my problem.
The difference is that the nodeset I want the position within is dynamic, rather than a contiguous section of the document.
To illustrate I'll modify the example from the linked question to match my requirements. Note that each <b> element is within a different <a> element. This is the critical bit.
<root
<a
<bzyx</b
</a
<a
<bwvu</b
</a
<a
<btsr</b
</a
<a
<bqpo</b
</a
</root
Now if I queried, using XPath for a/b I'd get a nodeset of the four <b> nodes. I want to then find the position within that nodeset of the node that contains the string 'tsr'. The solution in the other post breaks down here: count(a/b[.='tsr']/preceding-sibling::*)+1 returns 1 because preceding-sibling is navigating the document rather than the context node-set.
Is it possible to work within the context nodeset?