title: How to find nearest match from current context node
body: <p>I've got a rather large XML file that I'm trying to parse using a C# application and the HtmlAgilityPack. The XML looks something like this:</p>
  
  <pre><code>...
  &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;ABC-123&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;15&lt;/td&gt;
  &lt;td&gt;4&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;AB-4-320&lt;/td&gt;
  &lt;td&gt;11&lt;/td&gt;
  &lt;td&gt;2&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;b&gt;ABC-123&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;15&lt;/td&gt;
  &lt;td&gt;4&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;AB-4-320&lt;/td&gt;
  &lt;td&gt;11&lt;/td&gt;
  &lt;td&gt;2&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;CONTROLLER1&lt;/td&gt;
  &lt;td&gt;4&lt;/td&gt;
  &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;td&gt;CONTROLLER2&lt;/td&gt;
  &lt;td&gt;4&lt;/td&gt;
  &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
  ...
  </code></pre>
  
  <p>Basically a series of table rows and columns that repeats. I'm first doing a search for a controller by using:</p>
  
  <pre><code>string xPath = @"//tr/td[starts-with(.,'CONTROLLER2')]";
  HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(xPath);
  foreach (HtmlNode link in nodes) { ... }
  </code></pre>
  
  <p>Which returns the correct node. Now I want to search backwards (up) for the first (nearest) matching <code>&lt;td&gt;</code> node that starts with text "ABC":</p>
  
  <pre><code>string xPath = @link.XPath + @"/parent::tr/preceding-sibling::tr/td[starts-with(.,'ABC-')]";
  </code></pre>
  
  <p>This returns all matching nodes, not just the nearest one. When I attempted to add [1] to the end of this XPath string, it didn't seem to work and I've found no examples showing a predicate being used with an axes function like this. Or, more likely, I'm doing it wrong. Any suggestions?</p>