note 115957 added to simplexmlelement.xpath

From: Date: Sun, 19 Oct 2014 19:48:50 +0000
Subject: note 115957 added to simplexmlelement.xpath
Groups: php.notes php.notes 
Request: Send a blank email to php-notes+get-200684@lists.php.net to get a copy of this message
To run an xpath query on an XML document that has a namespace, the namespace must be registered with SimpleXMLElement::registerXPathNamespace() before running the query. If the XML document namespace does not include a prefix, you must make up an arbitrary one, and then use it in your query. <?php $strXml= <<<XML <?xml version="1.0" encoding="UTF-8"?> <mydoc xmlns="http://www.url.com/myns"> <message>Test message</message> </mydoc> XML; $xmlDoc=new \SimpleXMLElement($strXml); foreach($xmlDoc->getDocNamespaces() as $strPrefix => $strNamespace) { if(strlen($strPrefix)==0) { $strPrefix="a"; //Assign an arbitrary namespace prefix. } $xmlDoc->registerXPathNamespace($strPrefix,$strNamespace); } print($xmlDoc->xpath("//a:message")[0]); //Use the arbitrary namespace prefix in the query. ?> This will output: Test message ---- Server IP: 64.68.201.35 Probable Submitter: 68.146.192.112 ---- Manual Page -- http://php.net/manual/en/simplexmlelement.xpath.php Edit -- https://master.php.net/note/edit/115957 Del: integrated -- https://master.php.net/note/delete/115957/integrated Del: useless -- https://master.php.net/note/delete/115957/useless Del: bad code -- https://master.php.net/note/delete/115957/bad+code Del: spam -- https://master.php.net/note/delete/115957/spam Del: non-english -- https://master.php.net/note/delete/115957/non-english Del: in docs -- https://master.php.net/note/delete/115957/in+docs Del: other reasons-- https://master.php.net/note/delete/115957 Reject -- https://master.php.net/note/reject/115957 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#200684) next »