PHP File
Copy codeThe Code is as follows:
<? Php
Class xpathExtension {
Public static function getNodes ($ domDoc, $ xpathString ){
$ Xp = new DOMXPath ($ domDoc );
$ Xp-> registerNamespace ('x', 'HTTP: // www.w3.org/5o/xhtml ');
$ Xp-> registerNamespace ('xhtml ', 'HTTP: // www.w3.org/5o/xhtml ');
$ Xp-> registerNamespace ('i18n', 'HTTP: // apache.org/cocoon/i18n/2.1 ');
$ Ret = array ();
$ Nodes = $ xp-> query ($ xpathString );
Foreach ($ nodes as $ node ){
Array_push ($ ret, $ node );
}
Return $ ret;
}
}
$ DomDoc = new DOMDocument ();
$ DomDoc-> load ("x1.xml ");
$ XpathString = "// xml/products/product/description ";
$ DomNodeList = xpathExtension: getNodes ($ domDoc, $ xpathString );
// Echo count ($ domNodeList );
Foreach ($ domNodeList as $ domNode ){
Echo $ domNode-> nodeValue;
}
?>
Xml
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Xml>
<Products>
<Product>
<Description>
Abcd
</Description>
</Product>
</Products>
</Xml>