1

It's very easy to get an HTML node using Zend_Dom_Query by class:

$dom = new Zend_Dom_Query($html);
$rows = $dom->query("//div[@class='upc']");

However, HTML nodes often have more than one class, e.g.:

<div class="hidden upc">

How is it possible, using Zend_Dom_Query and XPath, to find all nodes which include a particular class in its list of classes, rather than having a specific value for the class attribute?

I have found an example that does this using Java, but applying it to the PHP Xpath engine doesn't seem to work as it gives an error that the XPath query is not valid:

//div[contains(concat(' ',normalize-space(@class),' '),' foo ')]
Charles
  • 50,943
  • 13
  • 104
  • 142
Jason
  • 14,517
  • 25
  • 92
  • 153
  • Are you searching for this? http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html – Emiliano Poggi May 06 '11 at 21:56
  • The example XPath that pads the value with spaces and uses `contains()` to look for an NMTOKEN also padded with spaces is the right way to go. It's a valid XPath expression. What error are you getting? – Mads Hansen May 07 '11 at 01:26
  • 2
    Aren't you supposed to be using `$dom->queryXpath(...)` instead of `$dom->query(...)` for an XPath expression? Also what's the exact error message you get from the last XPath expression you showed? – LarsH May 07 '11 at 02:44
  • 2
    Your question is why the "zend framework" cannot handle a syntactically valid XPath expression. This is not an XPath question at all. This would be an XPath question if you provided a wellformed XML file (as small as possible), the XPath expression and told us what the result was and what the result you're expecting really is, and asked for an explanation why the provided XPath expression doesn't select the expected nodes and what XPath expression to use that selects the expected nodes. Maybe this explains why 19 hours later you still have 0 answers? – Dimitre Novatchev May 07 '11 at 16:19

1 Answers1

1

I think you are looking for this to select the div with multiple classes:

$rows = $dom->query('div.hidden.upc');

vorbian
  • 11
  • 1