note 87141 deleted from function.simplexml-element-xpath by danbrown

From: Date: Wed, 16 Sep 2009 15:50:53 +0000
Subject: note 87141 deleted from function.simplexml-element-xpath by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160675@lists.php.net to get a copy of this message
Note Submitter: michael dot mauch at gmx dot de ---- Matt's comment is rather helpful, but that problem is not a fault of PHP, but a general problem with XPath and default namespaces. See e.g. http://www.edankert.com/defaultnamespaces.html and http://plasmasturm.org/log/259/ . If your XML doc has a default namespace (xmlns="some_namespace") and you want to use xpath, you have to register a prefix for the default namespace and use that prefix in all of your xpath expressions. <?php $xml_doc = <<<XML <?xml version='1.0'?> <!-- without namespace: --> <!-- <document> --> <!-- with default namespace: --> <document xmlns="uri:some_namespace"> <title>Forty What?</title> <from>Joe</from> <to>Jane</to> <body> I know that's the answer -- but what's the question? </body> </document> XML; $xml = simplexml_load_string($xml_doc); $namespaces = $xml->getNamespaces(true); if(isset($namespaces[""])) // if you have a default namespace { // register a prefix for that default namespace: $xml->registerXPathNamespace("default", $namespaces[""]); // and use that prefix in all of your xpath expressions: $xpath_to_document = "//default:document"; } else $xpath_to_document = "//document"; $document = $xml->xpath($xpath_to_document); print_r($document); ?>

« previous php.notes (#160675) next »