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']]
Share:
13,822

Related videos on Youtube

raph.amiard
Author by

raph.amiard

Updated on September 08, 2020

Comments

  • raph.amiard
    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 a property="p1" and the b node who has property="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
    Wrikken almost 14 years
    Indeed, spelled out it would read something like: //b[preceding-sibling::b/@property='p1' and following-sibling::b/@property='p2']
  • raph.amiard
    raph.amiard almost 14 years
    Wrikken : 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

Related