Benja0906 Benja0906 - 11 days ago 5
HTML Question

What is the right XPath expression?

Given the following HTML from a Wiki page:

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


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


AlBr3


I have tried:

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


But it just returns an error.

Answer

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).