28

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!

Rob
  • 631
  • 1
  • 7
  • 20
  • I haven't tried very much, only //div[contains(@class, 'result') and not contains(@class, 'grid')] and //div[contains(@class, 'result')][not contains(@class, 'grid')] but it was very apparent that my syntax was off right at the get go – Rob Jan 14 '13 at 18:38

3 Answers3

50

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";
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • 2
    This would also match anything with class = "*result" i.e. "someResult". So you gotta concat spaces to it. – crush Jan 14 '13 at 18:29
  • thanks for the comment crush, that is extremely helpful. as for the solution I'm certain this will work so I'll be giving credit once I'm at a computer and have verified the code. one last question: as a relatively new programmer, where should I be going for my solutions? I feel that asking questions on SO is lazy and that google provides messy resources. any advice? thanks again guys – Rob Jan 14 '13 at 18:43
  • Following @crush 's comment, I think the correct way is `//div[contains(concat(' ', normalize-space(@class), ' '), ' result ')]`, or `//div[@class and contains(concat(' ', normalize-space(@class), ' '), ' result ')]`. – Stéphane Laurent Nov 22 '19 at 20:31
13

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

crush
  • 16,713
  • 9
  • 59
  • 100
8

The XPATH syntax would be...

//div[not(contains(@class, 'grid'))]
CVEEP
  • 441
  • 4
  • 12