我正在尝试使用 xpath 来获取表格的内容。
桌子看起来像这样
<div>
<table>
<tr class="tableheader">
<td> Stuff </td>
</tr>
<tr class="indent1">
<td> Contents </td>
</tr>
<tr class="indent1">
<td> Contents </td>
</tr>
<tr class="tableheader">
<td> Stuff </td>
</tr>
<tr class="indent1">
<td> Contents </td>
</tr>
<tr class="indent1">
<td> Contents </td>
</tr>
</table>
</div>
我正在尝试在表头之间提取所有 tr[@class='indent1'] 的值
这就是我到目前为止所拥有的:
$elements = $xPath->query("div/table/tbody/tr[@class='tableheader']");
for ($i = 0; $i < $elements->length; $i++){
print "Node: ".$elements->item($i)->nodeValue."\n";
$siblings = $xPath->query("following-sibling::tr[@class='indent1']", $elements->item($i));
foreach ($siblings as $sibling) {
print "\tSibling: ".$sibling->nodeValue."\n";
}
}
预期的输出是
Node: Stuff
Sibling: Contents
Sibling: Contents
Node: Stuff
Sibling: Contents
Sibling: Content
s
相反,它为每个打印 ALL tr class="indent1s" 。
谢谢。