Over and over again, people ask questions about SimpleXML (the PHP built-in XML library which is, well, simple), because either:
- They don't know that
:
in a tag name denotes a namespace (e.g. most of these questions). - They know what a namespace is, but don't know how to use one with SimpleXML (e.g. most of these questions).
The problem is that most of these questions have accepted answers with either really bad advice (like mangling the XML with str_replace
before parsing it), or unnecessarily specific examples, so it's hard to find a good duplicate target.
For the record, there are two ways to use SimpleXML with namespaces: the ->children()
, and ->attributes()
methods to access the elements directly; or ->registerXpathNamespace()
if you want to use XPath.
I'd like to create a (Community Wiki) canonical Q&A for this, but don't want to go around dupe-hammering dozens of questions if people disagree, or think there is already a good target somewhere.