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