Benja0906 Benja0906 - 6 months ago 40
HTML Question

What is the right XPath expression?

Given the following HTML from a Wiki page:

<a href="/wiki/Aluminium_bromide" title="Aluminium bromide">Aluminium bromide</a> – AlBr

what should the XPath expression be, to be able to return:


I have tried:

string(//li[/a = "Aluminium bromide"]//-)

But it just returns an error.


That's a bit tricky because the way the nodes are nested.

But you could say "all text nodes inside the <li> that contains an <a> with a value of 'Aluminium bromide', except the text node of that <a> itself".

//li[a = 'Aluminium bromide']//text()[not(parent::a)]

and then do some post-processing (join, trim, etc).