代码示例:
$html = <<<END
<tr>
<td>Text-1</td>
</tr>
<tr>
<td>Blah 1</td>
<td>Blah 2</td>
<td>Blah 3</td>
<td>Blah 4</td>
</tr>
<tr>
<td>Text-2</td>
</tr>
<tr>
<td>Blah 1</td>
<td>Blah 2</td>
<td>Blah 3</td>
<td>Blah 4</td>
</tr>
END;
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadhtml($html);
$xpath = new DOMXPath($dom);
// Grab the text
$nodes = $xpath->query('//td[contains(text(), "Text-2")]|//td[contains(text(), "Blah 1")]/following-sibling::td');
echo $nodes->item(0)->textContent;
&#13;
我试图在Text-2下抓住Blah 2,问题是在Text-1下抓住了Blah 2
答案 0 :(得分:0)
首先,找到<tr>
孩子<td>
的{{1}},然后遍历下一个兄弟,直到找到"Text-2"
<td>
"Blah 1"
。答案就是下一个兄弟姐妹。
//tr[contains(td/text(), "Text-2")]/following-sibling::tr/td[contains(text(), "Blah 1")]/following-sibling::td'
答案 1 :(得分:0)
找到答案:
//*[text()='Text-2']/following::td[text()='Blah 1']/following-sibling::td