PHP getNamespaces() Function

In this chapter you will learn:

  1. Description for PHP getNamespaces() Function
  2. Syntax for PHP getNamespaces() Function
  3. Parameter for PHP getNamespaces() Function
  4. Return for PHP getNamespaces() Function
  5. Example - Return the namespaces used in the XML document

Description

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//j  a  v a 2s .com
$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.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP registerXPathNamespace() Function
  2. Syntax for PHP registerXPathNamespace() Function
  3. Parameter for PHP registerXPathNamespace() Function
  4. Return for PHP registerXPathNamespace() Function
  5. Example - Create a namespace context for the next XPath query
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