Search code examples
phpxmlnamespacessimplexmlxml-namespaces

Get Namespace Name in Root Element via PHP SimpleXMLElement


Assume I have the following XML (this is the standard for ATOM)

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

    <title>Example Feed</title>
    <subtitle>A subtitle.</subtitle>
    <link href="http://example.org/feed/" rel="self" />
    <link href="http://example.org/" />
    <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
    <updated>2003-12-13T18:30:02Z</updated>


    <entry>
        <title>Atom-Powered Robots Run Amok</title>
        <link href="http://example.org/2003/12/13/atom03" />
        <link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
        <link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/>
        <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
        <updated>2003-12-13T18:30:02Z</updated>
        <summary>Some text.</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>This is the entry content.</p>
            </div>
        </content>
        <author>
            <name>John Doe</name>
            <email>[email protected]</email>
        </author>
    </entry>

</feed>

Also, assume that the above XML is in a webpage whose address is http://www.example.com/atom.xml

I konw you can get the root element name via the follwoing code:

// Using curl to access the page
$ch = curl_init('http://www.example.com/atom.xml');
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

$xml = new SimpleXmlElement($result);
$root = $xml->getName();
echo ($root)  // This prints "feed", which is the root element

Also, I know that the following call will get all the root namespaces.

$xml->getNamespaces(true)

In our case, the return value is:

Array
(
    [] => http://www.w3.org/2005/Atom
)

However, I really like to know the name of the namespace. In other words, I do not know which function to call that returs xmlns. How do I know the name of the namespace? I need it to make some verifications before processing.

Please help, and thanks.


Solution

  • "However, I really like to know the name of the namespace. In other words, I do not know which function to call that returs xmlns".

    xmlns is XML syntax for default attribute name for namespace declaration. It isn't clear what you're trying to verify, but I think it is safe to assume that if getNamespaces() returns namespace which prefix is empty then the source XML has a valid default namespace (in other words, the source XML has xmlns).

    Quoting from W3C "Namespaces in XML" for easy reference :

    1. If the attribute name matches DefaultAttName, then the namespace name in the attribute value is that of the default namespace in the scope of the element to which the declaration is attached. In such a default declaration, the attribute value may be empty. Default namespaces and overriding of declarations are discussed in "5. Applying Namespaces to Elements and Attributes". [Default Namespace]

    2. [3] DefaultAttName ::= 'xmlns' . [Default Attribute Name]