DomXML xpath what do I do next?
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!!
</di开发者_如何学运维v>
</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?
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));
}
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.
精彩评论