PHP getDocNamespaces() Function

In this chapter you will learn:

  1. Description for PHP getDocNamespaces() Function
  2. Syntax for PHP getDocNamespaces() Function
  3. Parameter for PHP getDocNamespaces() Function
  4. Return for PHP getDocNamespaces() Function
  5. Example - Return the namespaces declared in the root of the XML document
  6. Example - Return all namespaces declared in parent and child nodes of the XML document

Description

The getDocNamespaces() function returns the namespaces declared in an XML document.

Syntax

PHP getDocNamespaces() Function has the following syntax.

getDocNamespaces(recursive,from_root);

Parameter

ParameterIs requiredDescription
recursiveOptional.A Boolean value. 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
from_rootOptional.A Boolean value. TRUE check namespaces from the root of the XML doc. FALSE check namespaces under a child node. Default is TRUE

Return

Returns an array of namespace names with their associated URIs

Example

Return the namespaces declared in the root of the XML document:


<?php/*from   ja va 2  s .  c  om*/
$xml=<<<XML
<?xml version="1.0" standalone="yes"?>
<books xmlns:c="http://php.net/ns">
  <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->getDocNamespaces();
print_r($ns);
?>

The code above generates the following result.

Example 2

Return all namespaces declared in parent and child nodes of the XML document:


<?php//from ja va2 s. c o m
$xml=<<<XML
<?xml version="1.0" standalone="yes"?>
<books xmlns:c="http://php.net">
  <c:book id="1">Java</c:book>
  <c:book id="2">PHP</c:book>
  <c:book id="3" a:language="English" xmlns:a="http://php.net">CSS</c:book> 
</books>
XML;

$sxe=new SimpleXMLElement($xml);
$ns=$sxe->getDocNamespaces(TRUE);
var_dump($ns);
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP getName() Function
  2. Syntax for PHP getName() Function
  3. Return for PHP getName() Function
  4. Example - Return the name of the XML element and the children
Home » PHP Tutorial » PHP SimpleXML Functions
PHP SimpleXMLElement Create
PHP addAttribute() Function
PHP addChild() Function
PHP asXML() Function
PHP attributes() Function
PHP children() Function
PHP SimpleXML count() Function
PHP getDocNamespaces() Function
PHP getName() Function
PHP getNamespaces() Function
PHP registerXPathNamespace() Function
PHP simplexml_import_dom() Function
PHP simplexml_load_file() Function
PHP simplexml_load_string() Function
PHP xpath() Function