0
votes

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?

1

1 Answers

3
votes

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.