note 102599 added to simplexml.examples-basic
[email protected] Wed, 23 Feb 2011 13:54:24 -0800
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
A quick tip on xpath queries and default namespaces. It looks like the XML-system behind SimpleXML has the same workings as I believe the XML-system .NET uses: when one needs to address something in the default namespace, one will have to declare the namespace using registerXPathNamespace and then use its prefix to address the otherwise in the default namespace living element.
<?php
$string = <<<XML
<?xml version='1.0'?>
<document xmlns="http://www.w3.org/2005/Atom">
<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($string);
$xml->registerXPathNamespace("def", "http://www.w3.org/2005/Atom");
$nodes = $xml->xpath("//def:document/def:title");
?>
----
Server IP: 69.147.83.197
Probable Submitter: 87.195.23.53
----
Manual Page -- http://www.php.net/manual/en/simplexml.examples-basic.php
Edit -- https://master.php.net/note/edit/102599
Del: integrated -- https://master.php.net/note/delete/102599/integrated
Del: useless -- https://master.php.net/note/delete/102599/useless
Del: bad code -- https://master.php.net/note/delete/102599/bad+code
Del: spam -- https://master.php.net/note/delete/102599/spam
Del: non-english -- https://master.php.net/note/delete/102599/non-english
Del: in docs -- https://master.php.net/note/delete/102599/in+docs
Del: other reasons-- https://master.php.net/note/delete/102599
Reject -- https://master.php.net/note/reject/102599
Search -- https://master.php.net/manage/user-notes.php