Returns the namespace declared in the root node of the XML document:
<?php$xml=<<<XML<?xml version="1.0" standalone="yes"?><cars xmlns:c="http://w3cschool.cc/ns"> <c:car id=" 1">Volvo</c:car> <c:car id="2">BMW</c:car> <c:car id="3">Saab</c:car> </cars>XML; $sxe=new SimpleXMLElement($xml);$ns=$sxe->getDocNamespaces();print_r($ns);?>The getDocNamespaces() function returns the namespaces declared in the XML document.
getDocNamespaces( recursive , from_root );
parameter | describe |
---|---|
recursive | Optional. Specifies a boolean value. If TRUE, returns all namespaces declared in all parent and child nodes. If the value is FALSE, only namespaces declared in the root node are returned. The default is FALSE. |
from_root | Optional. Specifies a boolean value. If the value is TRUE, the namespace in the root node of the XML document is checked. If the value is FALSE, the namespace under the child node is checked. Default is TRUE. |
Return value: | Returns an array of namespace names with associated URIs. |
---|---|
PHP version: | 5.1.2+ |
PHP change log: | PHP 5.4: Added from_root parameter. |
Returns all namespaces declared in the parent and child nodes of the XML document:
<?php$xml=<<<XML<?xml version="1.0" standalone="yes"?><cars xmlns:c="http://w3cschool.cc/ns"> <c:car id=" 1">Volvo</c:car> <c:car id="2">BMW</c:car> <c:car id="3" a:country="Sweden" xmlns:a="http://w3cschool.cc/country">Saab</c:car> </cars>XML;$sxe=new SimpleXMLElement($xml);$ns=$sxe->getDocNamespaces(TRUE) ;var_dump($ns);?>