标题总结了它。我正在尝试查询 HTML 文件以查找包含 classresult
且不包含 class的所有 div 标签grid
。
<div class="result grid">skip this div</div>
<div class="result">grab this one</div>
谢谢!
标题总结了它。我正在尝试查询 HTML 文件以查找包含 classresult
且不包含 class的所有 div 标签grid
。
<div class="result grid">skip this div</div>
<div class="result">grab this one</div>
谢谢!
这应该这样做:
<?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";
}
您的 XPath 将是//div[contains(concat(' ', @class, ' '), ' result ') and not(contains(concat(' ', @class, ' '), ' grid '))]
XPATH 语法将是...
//div[not(contains(@class, 'grid'))]