views:

21

answers:

2

I have this code:

$reader = new DOMDocument();
$reader->loadHTML($shell);
$xpath = new DomXPath($reader);
$xpath->registerNamespace('html','http://www.w3.org/1999/xhtml');
$res = $xpath->query('descendant-or-self::*[contains(@class,"content")]');
print_r($res);

$shell is just a variable containing the following html code:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Language" content="en-us" />

        <title>Hello World</title>
    </head>

    <body>
        <div class="content">
            Hello World!!
        </div>
    </body>
</html>

If I am correct the xpath query:

descendant-or-self::*[contains(@class,"content")]

is supposed to get the div with the class "content". However when I print the array all I see is an empty object:

DOMNodeList Object
(
)

Does this mean that the query didnt work? Is the DomXPath query language different to the SimpleXML Xpath one, because the query works with SimpleXML?

If it is working how do I view and modify the matched nodes?

A: 

I am thinking you want somthiing like this:

//*[@class='content']

This will get any tag with the class content.

It will be slightly more readable to just get the any div:

//div[@class='content']

In xpath, you use the // operator to grab a tag at any level in the dom. It will match them all.

Byron Whitlock
+2  A: 

print_r - ing a DOMNodeList (or any of the DOM-classes) doesn't do you much good: they're mostly implemented at C / Libxml2 level, and not natively exposed to PHP. As far as I can tell, this will work, add this after your query, and see if you get results:

foreach($res as $node){
        var_dump($node->ownerDocument->saveXML($node));
}
Wrikken
Thanks, The query is working. So how would I then go about editing the matched nodes attributes and data?
Franky Chanyau
I'd say look at the specification of `DOMElement` at http://www.php.net/manual/en/class.domelement.php, you're probably looking for the `setAttribute` function, the `childNodes` property (also a DOMNodeList), the `nodeValue` etc. The API takes some getting used to, but it is for the most part the agreed upon API, so any DOM primer (PHP or otherwise) would most likely give you a good insight in it's workings (and do check the comments in the PHP manual).
Wrikken