title: A CSS selector or xpath to iterate over rows with specific attributes
body: <p>Imagine I am given a table like this:</p>
  
  <pre><code>&lt;table&gt;
    &lt;tr&gt;&lt;td&gt;A&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;D&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;B&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I'd like to construct a CSS selector (preferred) or an XPath (accepted) that picks out the n'th row that contains an <code>a</code> anchor, such that:</p>
  
  <pre><code>selector(1) =&gt; &lt;a href="#"&gt;B&lt;/a&gt;
  selector(2) =&gt; &lt;a href="#"&gt;C&lt;/a&gt;
  selector(3) =&gt; &lt;a href="#"&gt;E&lt;/a&gt;
  </code></pre>
  
  <h3>CSS selectors</h3>
  
  <p>At this point, I'm pretty sure that CSS won't do the job, but</p>
  
  <pre><code>'table tr:nth-child(' + n + ')'
  </code></pre>
  
  <p>will pick out the n'th row, but that selects rows whether or not they have an <code>a</code> anchor.  Similarly, </p>
  
  <pre><code>'table tr:nth-child(' + n + ') a'
  </code></pre>
  
  <p>will pick out rows with an <code>a</code> anchor, but only if <code>n</code> is 2, 3 or 5.</p>
  
  <h3>XPath</h3>
  
  <p>With XPath, this matches all the <code>tr</code> that have an <code>a</code></p>
  
  <pre><code>`//table//tr//a/ancestor::tr`
  </code></pre>
  
  <p>but I can't figure out how to select the n'th match.  In particular,</p>
  
  <pre><code>`//table//tr//a/ancestor::tr[position() = 2]`
  </code></pre>
  
  <p>doesn't appear to select anything.</p>