php – 如何在DOMNode上执行XPath查询?

前端之家收集整理的这篇文章主要介绍了php – 如何在DOMNode上执行XPath查询?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法在DOMNode上执行xpath查询?或至少将其转换为DOMXPath?
<html>
  ...
  <div id="content">
     ...
     <div class="listing">
         ...
         <div></div>
         <div></div>
         <div  class='foo'>
           <h3>Get me 1</h3>
           <a>and me too 1</a>
         </div>
     </div>
     <div class="listing">
         ...
         <div></div>
         <div></div>
         <div class='foo'>
           <h3>Get me 2</h3>
           <a>and me too 1</a>
         </div>
     </div>
     ....
  </div>
</html>

这是我的代码我正在尝试获取一个数组的列表,其中包含h3的值和每个数组中的一个标签.为此,我需要获得每个列表,然后在每个列表中获取h3和标签的值.

$html_dom = new DOMDocument();
@$html_dom->loadHTML($html);
$x_path = new DOMXPath($html_dom);

$nodes= $x_path->query("//div[@id='content']//div[@class='listing']");

foreach ($nodes as $node)
{
  // I want to further dig down here using query on a DOMNode
}
将节点作为第二个参数传递给 DOMXPath::query

contextnode: The optional contextnode can be specified for doing relative XPath queries. By default,the queries are relative to the root element.

例:

foreach ($nodes as $node) {
    foreach ($x_path->query('h3|a',$node) as $child) {
        echo $child->nodeValue,PHP_EOL;
    }
}

这使用UNION operator的结果

Get me 1
and me too 1
Get me 2
and me too 1

如果你不需要任何复杂的查询,你也可以做

foreach ($nodes as $node) {
    foreach ($node->getElementsByTagName('a') as $a) {
      echo $a->nodeValue,PHP_EOL;
    }
}

或者甚至通过迭代子节点(请注意,这包括所有文本节点)

foreach ($nodes as $node) {
    foreach ($node->childNodes as $child) {
      echo $child->nodeName,PHP_EOL;
    }
}

但是,所有这些都是不必要的,因为您可以直接获取这些节点:

$nodes= $x_path->query("/html/body//div[@class='listing']/div[last()]");

foreach ($nodes as $i => $node) {
    echo $i,$node->nodeValue,PHP_EOL;
}

将给所有div的最后一个div子节点中的两个节点列出一个类属性值,并输出组合的文本节点值,包括空格

0
           Get me 1
           and me too 1

1
           Get me 2
           and me too 1

同样,以下

"//div[@class='listing']/div[last()]/node()[name() = 'h3' or name() = 'a']"

将给你四个孩子H3和A节点并输出

0Get me 1
1and me too 1
2Get me 2
3and me too 1

如果您需要通过名称来区分这些名称,然后迭代它们就可以

foreach ($nodes as $i => $node) {
    echo $i,$node->nodeName,PHP_EOL;
}

然后给予

0h3Get me 1
1aand me too 1
2h3Get me 2
3aand me too 1
原文链接:https://www.f2er.com/php/140210.html

猜你在找的PHP相关文章