CHAPTER 8 – XML with PHP 5 – Using XPath
By using XPath, we can further simplify the previous example. XPath is a query language for XML documents, and it is also used in XSLT for matching nodes. We can use XPath to query a DOM document for certain nodes and attributes, similar to using SQL to query a database: 1 <?php 2 $dom = new DomDocument(); 3 $dom->load('test2.xml'); 4 $xpath = new DomXPath($dom); 5 $nodes = $xpath->query("*[local-name()='body']", $dom ->documentElement); 6 echo $nodes->item(0)->getAttributeNode('background')->value. "n"; 7 ?>