PHP xpath 包含类,不包含类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14324179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:04:43  来源:igfitidea点击:

PHP xpath contains class and does not contain class

phpxpathdomxpathxpathquery

提问by Rob

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

标题总结了它。我正在尝试为包含 classresult和不包含 class 的所有 div 标签查询 HTML 文件grid

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

Thanks!

谢谢!

回答by Sean Bright

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";
}

回答by crush

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

你的 XPath 将是 //div[contains(concat(' ', @class, ' '), ' result ') and not(contains(concat(' ', @class, ' '), ' grid '))]

回答by wlvrn

The XPATH syntax would be...

XPATH 语法将是...

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