In XPath 1.0 one can use the general (Kayessian) method for intersection of two node-sets $ns1
and $ns2
:
$ns1[count(.|$ns2) = count($ns2)]
When we replace $ns1
and $ns2
with their specific selecting expressions for this particular case, we get:
/*/comment()[1]/following-sibling::div
[count(. | /*/comment()[2]/preceding-sibling::div)
=
count(/*/comment()[2]/preceding-sibling::div)
]
XSLT-based verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/comment()[1]/following-sibling::div
[count(. | /*/comment()[2]/preceding-sibling::div)
=
count(/*/comment()[2]/preceding-sibling::div)
]
"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
<div>
<div></div>
<div></div>
<!--Comment1-->
<div>1</div>
<div>2</div>
<div>3</div>
<!--Comment2-->
<div></div>
<div></div>
<div></div>
</div>
The exact wanted nodes are selected and output:
<div>1</div>
<div>2</div>
<div>3</div>
In XPath 2.0 one uses the intersect
operator:
/*/comment()[1]/following-sibling::div
intersect
/*/comment()[2]/preceding-sibling::div