title: XPath Expression: Select elements between A HREF="expr" tags
body: <p>I didn't found an explicit way to select all nodes that exist between two anchors (<code>&lt;a&gt;&lt;/a&gt;</code> tag pair) in an HTML file. </p>
  
  <p>The first anchor has the following format:</p>
  
  <pre><code>&lt;a href="file://START..."&gt;&lt;/a&gt;
  </code></pre>
  
  <p>Second anchor:</p>
  
  <pre><code>&lt;a href="file://END..."&gt;&lt;/a&gt;
  </code></pre>
  
  <p>I've verified that both can be selected using starts-with (note that I'm using HTML Agility Pack):</p>
  
  <pre><code>HtmlNode n0 = html.DocumentNode.SelectSingleNode("//a[starts-with(@href,'file://START')]"));
  HtmlNode n1 = html.DocumentNode.SelectSingleNode("//a[starts-with(@href,'file://END')]"));
  </code></pre>
  
  <p>With this in mind, and with my amateurish XPath skills, I wrote the following expression to get all tags between the two anchors:</p>
  
  <pre><code>html.DocumentNode.SelectNodes("//*[not(following-sibling::a[starts-with(@href,'file://START0')]) and not (preceding-sibling::a[starts-with(@href,'file://END0')])]");
  </code></pre>
  
  <p>This seems to work, but selects all HTML document! </p>
  
  <p>I need to, for example for the following HTML fragment:</p>
  
  <pre><code>&lt;html&gt;
  ...
  
  &lt;a href="file://START0"&gt;&lt;/a&gt;
  &lt;p&gt;First nodes&lt;/p&gt;
  &lt;p&gt;First nodes
      &lt;span&gt;X&lt;/span&gt;
  &lt;/p&gt;
  &lt;p&gt;First nodes&lt;/p&gt;
  &lt;a href="file://END0"&gt;&lt;/a&gt;
  
  ...
  &lt;/html&gt;
  </code></pre>
  
  <p>remove both anchors, the three P (including of course the inner SPAN).</p>
  
  <p>Any way to do this? </p>
  
  <p>I don't know if XPath 2.0 offers better ways to achieve this.</p>
  
  <p>*<strong>EDIT (special case!) *</strong></p>
  
  <p>I should also handle the case where:</p>
  
  <p>"Select tags between X and X', where X is <code>&lt;p&gt;&lt;a href="file://..."&gt;&lt;/a&gt;&lt;/p&gt;</code>"</p>
  
  <p>So instead of:</p>
  
  <pre><code>&lt;a href="file://START..."&gt;&lt;/a&gt;
  &lt;!-- xhtml to be extracted --&gt;
  &lt;a href="file://END..."&gt;&lt;/a&gt;
  </code></pre>
  
  <p>I should handle also:</p>
  
  <pre><code>&lt;p&gt;
    &lt;a href="file://START..."&gt;&lt;/a&gt;
  &lt;/p&gt;
  &lt;!-- xhtml to be extracted --&gt;
  
  &lt;p&gt;
    &lt;a href="file://END..."&gt;&lt;/a&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>Thank you very much, again.</p>