如何为每次匹配仅使用xpath选择文本字符串的直接父节点 [英] How can I select only the immediate parent node of a text string using xpath for every match
本文介绍了如何为每次匹配仅使用xpath选择文本字符串的直接父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
注意:这与以下问题不同,在这里,我们的值出现在同一节点的节点内和子节点内:
Note: this differs from the following question in that here we have values appearing within a node and within a childnode of that same node:
XPath contains(text(),'some string')当与具有多个Text子节点的节点一起使用时,则不起作用
给出以下html:
$content =
'<html>
<body>
<div>
<p>During the interim there shall be nourishment supplied</p>
</div>
<div>
<p>During the <a href="#">interim</a> there shall be interim nourishment supplied</p>
</div>
<div>
<ul><li>During the interim there shall be nourishment supplied</li></ul>
</div>
</body>
</html>';
以及以下xpath:
//*[contains(text(),'interim')]
...只提供3个匹配项,而我想要4个匹配项。根据评论,我期望的四个元素是P P A LI。
... only provides 3 matches, whereas I want four matches. As per comments, the four elements I'm expecting are P P A LI.
推荐答案
这完全符合预期。请参见此 glot.io链接。
This works exactly as expected. See this glot.io link.
<?php
$html = <<<HTML
<html>
<body>
<div>
<p>During the interim there shall be nourishment supplied</p>
</div>
<div>
<p>During the <a href="#">interim</a> there shall be interim nourishment supplied</p>
</div>
<div>
<ul><li>During the interim there shall be nourishment supplied</li></ul>
</div>
</body>
</html>
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach($xpath->query('//*/text()[contains(.,"interim")]') as $n) var_dump($n->getNodePath());
您将获得四场比赛:
- / html / body / div [1] / p / text()
- / html / body / div [2] / p / a / text()
- / html / body / div [2] / p / text()[2]
- / html / body / div [3] / ul / li / text()
- /html/body/div[1]/p/text()
- /html/body/div[2]/p/a/text()
- /html/body/div[2]/p/text()[2]
- /html/body/div[3]/ul/li/text()
这篇关于如何为每次匹配仅使用xpath选择文本字符串的直接父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文