PHP getDocNamespaces()Functions
PHP SimpleXML Reference Manual
Examples
Return the namespaces 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);
?>
$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);
?>
Run Example »
Definition and Usage
The getDocNamespaces() function returns the namespaces declared in the XML document.
Syntax
getDocNamespaces(recursive,from_root);
| Parameters | Description |
|---|---|
| recursive | Optional. Specifies a boolean value. If the value is TRUE, returns all namespaces declared in all parent and child nodes. If the value is FALSE, returns only the namespaces declared in the root node. Default is FALSE. |
| from_root | Optional. Specifies a boolean value. If the value is TRUE, checks the namespaces in the root node of the XML document. If the value is FALSE, checks the namespaces under the child nodes. Default is TRUE. |
Technical Details
| Return value: | Returns an array of namespace names with their associated URIs. |
|---|---|
| PHP Version: | 5.1.2+ |
| PHP Changelog: | PHP 5.4: Added thefrom_rootparameter. |
More Examples
Example 1
Return 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);
?>
$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);
?>
Run Example »
PHP SimpleXML Reference Manual Other Extensions