I didn’t found an explicit way to select all nodes that exist between two anchors (<a></a> tag pair) in an HTML file.
The first anchor has the following format:
<a href="file://START..."></a>
Second anchor:
<a href="file://END..."></a>
I’ve verified that both can be selected using starts-with (note that I’m using HTML Agility Pack):
HtmlNode n0 = html.DocumentNode.SelectSingleNode("//a[starts-with(@href,'file://START')]"));
HtmlNode n1 = html.DocumentNode.SelectSingleNode("//a[starts-with(@href,'file://END')]"));
With this in mind, and with my amateurish XPath skills, I wrote the following expression to get all tags between the two anchors:
html.DocumentNode.SelectNodes("//*[not(following-sibling::a[starts-with(@href,'file://START0')]) and not (preceding-sibling::a[starts-with(@href,'file://END0')])]");
This seems to work, but selects all HTML document!
I need to, for example for the following HTML fragment:
<html>
...
<a href="file://START0"></a>
<p>First nodes</p>
<p>First nodes
<span>X</span>
</p>
<p>First nodes</p>
<a href="file://END0"></a>
...
</html>
remove both anchors, the three P (including of course the inner SPAN).
Any way to do this?
I don’t know if XPath 2.0 offers better ways to achieve this.
*EDIT (special case!) *
I should also handle the case where:
“Select tags between X and X’, where X is <p><a href="file://..."></a></p>“
So instead of:
<a href="file://START..."></a>
<!-- xhtml to be extracted -->
<a href="file://END..."></a>
I should handle also:
<p>
<a href="file://START..."></a>
</p>
<!-- xhtml to be extracted -->
<p>
<a href="file://END..."></a>
</p>
Thank you very much, again.
Use this XPath 1.0 expression:
Or, use this XPath 2.0 expression:
The XPath 2.0 expression uses the XPath 2.0
intersectoperator.The XPath 1.0 expression uses the Kayessian (after @Michael Kay) formula for the intersectioon of two node-sets:
Verification with XSLT:
This XSLT 1.0 transformation:
when applied on the provided XML document:
produces the wanted, correct result:
This XSLT 2.0 transformation:
when applied on the same XML document (above) again produces exactly the wanted result.