The getNamespaces() function returns the namespaces used in an XML document.
PHP getNamespaces() Function has the following syntax.
getNamespaces(recursive);
Parameter | Is Required | Description |
---|---|---|
recursive | Optional. | If TRUE, all namespaces declared in parent and child nodes are returned. If FALSE, only namespaces declared in root node is returned. Default is FALSE |
Returns an array of namespace names with their associated URIs
Return the namespaces used in the XML document:
<?php/*from w ww . ja v a2s . co m*/
$xml=<<<XML
<?xml version="1.0" standalone="yes"?>
<books xmlns:c="http://book.com/ns" xmlns:a="http://book.com/country">
<c:book id="1">Java</c:book>
<c:book id="2">PHP</c:book>
<c:book id="3">CSS</c:book>
</books>
XML;
$sxe=new SimpleXMLElement($xml);
$ns=$sxe->getNamespaces(true);
var_dump($ns);
?>
The code above generates the following result.