PHP DOMXPath always returning empty object


#1

I’m new to DOMXPath, and am basing off of examples I’ve found on the web, but no matter what page I try to parse (my own sites, google groups), I’m getting an empty DOMXPath object.

[php]
// parse page
$dom = new DOMDocument();
if (! @$dom->loadHTML($html))
return FALSE;
echo $dom->saveHTML();

$xpath = new DOMXPath($dom);

$data[‘jdata’] = var_dump($xpath);
$elements = $xpath->query("/html");
$data[‘jdata’] = var_dump($elements);
[/php]

The echo $dom->saveHTML() call shows the html, but the var_dumps produce:

object(DOMXPath)#27 (0) {
}
object(DOMNodeList)#29 (0) {
}

Any ideas?

Thanks


#2

Everything you’re doing is fine, with one silly exception — var_dump doesn’t display any useful output on DOM objects. You’ll need to pull out the nodes from the DOMNodeList and look at them yourself.


#3

Thanks, Andrew!

I sort of stumbled upon that tidbit myself, subsequent to posting. (I also should have paid more attention to where/how DOMXPath fit into the DOM/XML hierarchy when I first looked at the manual page.)

btw - do you know of a good reference and/or tutorial for XPATH queries?