How I select everything (all possible nodes) between the 1st and second h2? There can be n nodes between them, and there can be m h2 tags.
The nodes aren’t necessarily going to be contained in an HTML elment, so the selector can just grab them all.
<html>
<h2>asdf</h2>
<p>good stuff 1</p>
<p>good stuff 2</p>
<p>good <a href="#">asdf</a>stuff n...</p>
<h2>qwer</h2>
<p>test2</p>
<h2>dfgh</h2>
<p>test2</p>
</html>
I’m just getting my feet wet with XPath. Please help my newbie question 🙂
Thanks so much!
One XPath expression that selects the wanted elements is:
In general, in such cases one can use the Kayessian formula for set intersection:
This XPath expression selects all the nodes that belong both to the nodesets
$ns1and$ns2.If you want to get all nodes between two given nodes $n1 and $n2, this is the intersection of two nodesets:
$n1/following-sibling::node()and$n2/preceding-sibling::node().Just substitute these expression into the Kayessian formula and you have the wanted XPath expression.
In XPath 2.0, of course, one would use the
<<or>>operator, something like: