XPath to separately select each of two values in a table cell?

Viewed 51
<td _ngcontent-wp class="align-middle">
 "4.79728"
  <small _ngcontent-wp class="neo_red_dark"> -0.08% </small>
</td>

My XPath as follows:

(//table[@class="table"]/tbody/tr/td[3])[1]

It works, but it gets two values together (4.79728 -0.08%). How can I get them separately?

2 Answers

You can get the value before the space and after the space using:

substring-before() and substring-after()

or change your XPath to target the text() descendants of the td instead of the td itself (which is producing the calculated text value).

In order to select "4.79728":

(//table[@class="table"]/tbody/tr/td[3])[1]/text()

In order to select -0.08%:

(//table[@class="table"]/tbody/tr/td[3])[1]/small/text()

You should indicate with XPath questions which XPath version you are using.

If it's version 1.0, remember that the set of data types you can return is very limited: a single string, number, or boolean, or a node-set. And some APIs only allow you to return a node-set.

Your current query is returning a node-set containing one node, namely a td element, whose string value contains the concatenation of all the text within. You could return a node-set containing all the text nodes individually by appending //text() to the query. But of course, it won't always be the case that the two numbers are in separate text nodes.

Related