$xml = new SimpleXMLElement($xml_string); $xml->registerXPathNamespace('ns', 'http://www.example.com'); $result = $xml->xpath('//ns:element');
$doc = new DOMDocument(); $doc->load('file.xml'); $xml = simplexml_import_dom($doc); $xml->registerXPathNamespace('x', 'http://www.example.com'); $result = $xml->xpath('//x:element');In this example, an XML document is loaded using the DOMDocument class and turned into a SimpleXMLElement object using the simplexml_import_dom function. A namespace with the prefix 'x' and URI 'http://www.example.com' is registered with the SimpleXMLElement object, and the XPath expression '//x:element' is then used to select all 'element' nodes in the 'http://www.example.com' namespace. This function is part of the PHP standard library, which comes bundled with PHP.