htmlxpathdomxpathxpathquery

Is there anything similar to an if-else in XPath?


I want to check for a certain text in HTML, such as 'No Value', and if that exists then assign a 0 to it. Does anybody have any idea how to do that? (Google a lot but didn't find anything relevant)

<table>
<tr>
<td>
<h3>Money in account</h3>
</td>
<td>
<p>No Value</p>
</td>
</tr>
</table>

Solution

  • In XPath 2.0:

    if (p = 'No Value') then 0 else p
    

    In XPath 1.0 there's no obvious solution but there are some convoluted workarounds: it depends a bit on the context in which you are working (e.g. can you declare variables).

    It's time to move to XPath 2.0.