我如何 select 仅对每个匹配项使用 xpath 的文本字符串的直接父节点

How can I select only the immediate parent node of a text string using xpath for every match

注意:这与以下问题的不同之处在于,这里我们的值出现在一个节点内以及同一节点的子节点内:

XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

给出以下 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 场比赛,而我想要四场比赛。根据评论,我期待的四个元素是 P P A LI。

这完全符合预期。请参阅 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()