I have XML-structure. Context of executing XPath is one of the elements with class attribute “ac”. Help me to compose XPath to find all immediate siblings of context element with attribute class “ac1”. E.g. if context of execution is the second element with class attribute “ac”, the result should contain two (not three) immediate elements with class attribute “ac1”.
Thank you.
<container>
<item class="ac"/>
<item class="ac1"/>
<item class="ac1"/>
<item class="ac1"/>
<item class="ac"/>
<item class="ac1"/>
<item class="ac1"/>
<item class="ac2"/>
<item class="ac1"/>
<item class="ac"/>
<item class="ac1"/>
<item class="ac1"/>
<item class="ac1"/>
</container>
I. In XPath 1.0 use the Kayessian method for the intersection of two node-sets:
This selects the intersection of the two node-sets
$ns1and$ns2.Substitute in this :
$ns1with:and
$ns2with:The resulting XPath expression is:
XSLT – based verification:
When this transformation is applied on the provided XML document:
the two XPath expressions (one with variables and the other with the variables substituted) are evaluated and the selected nodes by each of them are copied to the output:
II. XPath 2.0 Solution:
Simply use the standard XPath 2.0
intersectoperator with the same two node-sets.Alternatively one can use the standard operators
<<and>>.