PHP Tutorial - PHP getNamespaces() Function






The getNamespaces() function returns the namespaces used in an XML document.

Syntax

PHP getNamespaces() Function has the following syntax.

getNamespaces(recursive);

Parameter

ParameterIs RequiredDescription
recursiveOptional.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

Return

Returns an array of namespace names with their associated URIs

Example

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.