0

Following the post here Combining the use of preceding and following sibling in the same xpath query I am wanting to do the same except I want to get nodes up to the first occurrence of p2.

So the modified example data is

<a>
    <b property="p1">zyx</b>
    <b>wvu</b>
    <b>tsr</b>
    <b>dcv</b> 
    <b property="p2">qpo</b>
    <b>qcs</b>
    <b property="p2">wee</b>
    <b>tbg</b>
    <b>rty</b>
    <b property="p2">qwe</b>
    <b>jkl</b>
</a>

I have tried

/a/b[preceding-sibling::b/@property='p1' and following-sibling::b[1]/@property='p2']

which gives me dcv, qcs, and rty. Then I tried

/a/b[preceding-sibling::b/@property='p1' and (following-sibling::b/@property='p2')[1]]

which gives me wvu, tsr, dcv, qpo, qcs, wee, tbg, and rty.

What I actually need is from p1 to the first p2 or wvu, tsr, and dcv. I have tried almost every combination of [1] but have failed and would like some help on the construct.

zx485
  • 28,498
  • 28
  • 50
  • 59
sinDizzy
  • 1,300
  • 7
  • 28
  • 60

1 Answers1

0

One way to achieve this is by using the following XPath-1.0 expression:

/a/b[preceding-sibling::b/@property='p1' and not(preceding-sibling::b/@property='p2' or @property='p2')]

It checks if a given b element has a preceding-sibling named p1 and is not a preceding-sibling of p2 or p2 itself. That's all.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • Very clever answer. Being relatively new to xpath i didn't know how to put it together. Many thanks. – sinDizzy Nov 15 '18 at 23:35