PHP xpath contains class and does not contain class

北城以北 提交于 2019-12-18 11:45:27

问题


The title sums it up. I'm trying to query an HTML file for all div tags that contain the class result and does not contain the class grid.

<div class="result grid">skip this div</div>
<div class="result">grab this one</div>

Thanks!


回答1:


This should do it:

<?php
$doc = new DOMDocument();
$doc->loadHTMLFile('test.html');

$xpath = new DOMXPath($doc);
$nodeList = $xpath->query(
    "//div[contains(@class, 'result') and not(contains(@class, 'grid'))]");

foreach ($nodeList as $node) {
  echo $node->nodeName . "\n";
}



回答2:


Your XPath would be //div[contains(concat(' ', @class, ' '), ' result ') and not(contains(concat(' ', @class, ' '), ' grid '))]




回答3:


The XPATH syntax would be...

//div[not(contains(@class, 'grid'))]


来源:https://stackoverflow.com/questions/14324179/php-xpath-contains-class-and-does-not-contain-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!