Combining the use of preceding and following sibling in the same xpath query
13,822
XPath 1.0:
/a/b[preceding-sibling::b/@property='p1' and following-sibling::b/@property='p2']
XPath 2.0:
The expression above has some quirks in XSLT 2.0, it is better to use the new and safer operators <<
(before) and >>
(after).
/a/b[../b[@property='p2'] << . and . >> ../b[@property='p1']]
Related videos on Youtube
Author by
raph.amiard
Updated on September 08, 2020Comments
-
raph.amiard almost 4 years
I have a quite simple problem but i can't seem to resolve it. Let's say i have the following code:
<a> <b property="p1">zyx</b> <b>wvu</b> <b>tsr</b> <b property="p2">qpo</b> <b>qcs</b> </a>
I want to select the nodes between the
b
node who has aproperty="p1"
and theb
node who hasproperty="p2"
. I can do either of those with the preceding-sibling and the following-sibling axis but I can't seem to find how to combine both. -
Wrikken almost 14 yearsIndeed, spelled out it would read something like:
//b[preceding-sibling::b/@property='p1' and following-sibling::b/@property='p2']
-
raph.amiard almost 14 yearsWrikken : ok thank you. I didn't know i could use preceding and following sibling like that, i only ever saw it used like "b[@property="p1"]/following-sibling::*" Paul Butcher : i knew that, but thank you for taking the time to answer