theta
theta

Reputation: 25591

Select all elements between two nodes on same level

...
<h>unique-1</h>
<a>data</a>
<b>data</b>
<a>data</a>
<h>unique-2</h>
...

I need all data between two h nodes. I can point to h node with //h[.='unique-1'] but can't find reference how to select all nodes between <h>unique-1</h> and <h>unique-2</h>

Upvotes: 2

Views: 480

Answers (1)

Fred Foo
Fred Foo

Reputation: 363467

In XPath 2.0, you can use the intersect operator in conjunction with the following-sibling and preceding-sibling axes:

          //h[.='unique-1']/following-sibling::*
intersect //h[.='unique-2']/preceding-sibling::*

In XPath 1.0, you can simulate the intersect operator with a "Kaysian intersection":

//h[.='unique-1']/following-sibling::*[
  count(.|//h[.='unique-2']/preceding-sibling::*)
  = count(//h[.='unique-2']/preceding-sibling::*)
]

Upvotes: 2

Related Questions