Prinple Vrlo
Prinple Vrlo

Reputation: 79

How to get elements between tags with XPATH

I need to get each subtitle of an article and its text. Since each subheading is inside , and I need to get everything between the first and the second. And then I will do between the second and third until I finish.

The structure is similar to this:

<article>
<p> introducion </p>

<h3>1. Subtitle </h3>
<p> text text </p>
<div> <p>other text</p> </div>

<h3>2. Subtitle </h3>
<p> text text </p>
<div> <p>other text</p> </div>

<h3>3. Subtitle </h3>
<p> text text </p>
<div> <p>other text</p> </div>

</article>

Currently I can get to the first subtitle like this: //h3[1]

But how can I get everything between the first and the second ???

Upvotes: 0

Views: 72

Answers (1)

LMC
LMC

Reputation: 12662

This XPath expression gets nodes between //h3[1] and //h3[2] inclusive

//article/*[position()>= count(//h3[1]/preceding-sibling::*)+1 and position()<= count(//h3[2]/preceding-sibling::*)+1]

Result on browser console

$x('//article/*[position()>= count(//h3[1]/preceding-sibling::*)+1 and position()<= count(//h3[2]/preceding-sibling::*)+1]')
Array(4) [ h3, p, div, h3]
0: <h3>​
1: <p>​
2: <div>​
3: <h3>
length: 4

Upvotes: 1

Related Questions