One XPath expression that selects the required elements :
h2[2]/preceding-sibling::p) = count(
In the general case, in such cases, you can use the Kaisey formula for many intersections:
$ns1[count(.|$ns2) = count($ns2)]
This XPath expression selects all nodes belonging to both $ns1 and $ns2 .
If you want to get all the nodes between two given nodes $ n1 and $ n2, this will be the intersection of two nodes: $n1/following-sibling::node() and $n2/preceding-sibling::node() .
Just replace this expression with the Kayessian formula, and you have the required XPath expression.
In XPath 2.0 , of course, you can use the << or >> operator, for example:
/*/h2[1]/following-sibling::p[. << /*/h2[1]/]
source share