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>
<b>zyx</b>
</a>
<a>
<b>wvu</b>
</a>
<a>
<b>tsr</b>
</a>
<a>
<b>qpo</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?
The reason you are getting 1 is nothing to do with context vs. document, but because you are only counting
bnodes within the oneanode (so you will always get a count of 0 because there are never any preceding 'b' nodes.Rather you need to find the count of preceding 'a' nodes before the 'b' that contains your 'a'.
Something like: