Geveze
Geveze

Reputation: 391

Xpath all text before 4th br tag

I have this HTML:

<div>
here a long text1 ...
<br>
<br>
here some more text2 ...
<br>
<br>
Here also text3 ..
<br>
<br>
Here text4 and so forth
</div>

I only want the text before the 4th br tag and maybe also the br tags itself. Like this:

here a long text1 ...
<br>
<br>
here some more text2 ...
<br>
<br>

What is the correct xpath for this?

Upvotes: 0

Views: 3620

Answers (1)

Tomalak
Tomalak

Reputation: 338326

With elements and text nodes:

/div/node()[count(preceding-sibling::br) < 5]

With text nodes only:

/div/text()[count(preceding-sibling::br) < 5]

In any case you will get a few empty text nodes with both expressions - the ones between the <br> elements.

Upvotes: 3

Related Questions