fractal5 fractal5 - 1 month ago 5
HTML Question

How to get all childs of a node using php xpath?

In the code below:

<tbody>
<tr class='even'>
<th>header 1</th>
<td>11</td>
<td>12</td>
</tr>
<tr class='even'>
<th>header 2</th>
<td>21</td>
<td>22</td>
</tr>
</tbody>


When I run,
$xpath->query("//tr[@class='even']")


It only shows me the contents of the
<th>
tags. Shouldn't I be getting the contents of the
<td>
as well i.e. everything inside the
<tr>
tags?

If not how can I get the td without having to write separate ones like this:

$xpath->query("//tr[@class='even']/th")
$xpath->query("//tr[@class='even']/td")

Answer

You can use * selector that select any tag.

$xpath->query("//tr[@class='even']/*")

The above code select every child of tr.even. Also you can use | (OR) operator like bottom code.

$xpath->query("//tr[@class='even']/td | //tr[@class='even']/th");