Marko Galinec Marko Galinec - 6 months ago 23
HTML Question

Xpath selecting nodes with text

I have 2 requests.

First i need xpath expression that selects all

<tr>
elements with any kind of text nested in that element.
I tried with :

//tr[contains(., 'PRVI ODJELJAK')]/following-sibling::tr[text() != '']


but it doesnt work it still selects siblings w/o a text also. :/

secondly is there a way to lets say select all siblings of an element until you hit a sibling with inner text matching some text.

Thx in advance!

Answer

"First i need xpath expression that selects all elements with any kind of text nested in that element"

To filter element that contains some non-whitespace text in it, you can use normalize-space() :

//tr[contains(., 'PRVI ODJELJAK')]/following-sibling::tr[normalize-space()]

"secondly is there a way to lets say select all siblings of an element until you hit a sibling with inner text matching some text"

Probably you can emulate that by selecting siblings where there is following-sibling with inner text matching some text

following-sibling::tr[following-sibling::tr[contains(.,'some text')]]