PHP SimpleXMLElement::getDocNamespaces() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::getDocNamespaces() returns the namespaces that are declared in an XML document.

The result is an array where each key is a prefix and each value is the namespace URI. It lists namespaces that are declared with xmlns, even if no element uses them yet.

Syntax

Call SimpleXMLElement::getDocNamespaces() with the values you need. Start with this line.

PHP
$namespaces = $root->getDocNamespaces();
CallWhat it does
$root->getDocNamespaces()Returns the namespaces declared on the root element.
$root->getDocNamespaces(true)Also looks into all child elements for xmlns declarations.
$root->item->getDocNamespaces(false, false)Starts from the current element instead of the document root.

A default namespace (xmlns="...") appears with an empty string as its key. An XML document without any namespace gives an empty array.

Library declarations
PHP
SimpleXMLElement::getDocNamespaces(bool $recursive = false, bool $fromRoot = true): array|false

Parameters

ParameterDescription
recursiveOptional. Set to true to also return namespaces declared on child elements. The default is false.
fromRootOptional. When true, the search starts at the document root. When false, it starts at the current element. The default is true.

Return Value

Return ValueDescription
arrayAn array of prefix => URI pairs. It is empty when no namespaces are declared.
falseReturned on failure.

PHP Examples

Example 1 — List the Declared Namespaces

Loop over the result to print each prefix and its URI.

PHP
<?php
$xml = '<root xmlns:x="urn:x"><item>Tea</item></root>';
$root = simplexml_load_string($xml);

foreach ($root->getDocNamespaces() as $prefix => $uri) {
    echo $prefix, " => ", $uri, PHP_EOL;
}

How It Works

  1. The root element declares one namespace with the prefix x.
  2. No element uses the prefix, but it is still declared.
  3. getDocNamespaces() returns an array with x as the key and urn:x as the value.
  4. foreach prints the prefix and the URI.

Example 2 — No Namespaces Gives an Empty Array

A simple document with no xmlns attribute returns an empty array, not false.

PHP
<?php
$book = simplexml_load_string('<book><title id="1">PHP</title></book>');

var_dump($book->getDocNamespaces());

How It Works

  1. The book XML has no xmlns declarations at all.
  2. getDocNamespaces() finds nothing to report.
  3. It returns an empty array.
  4. var_dump() shows array(0) with no items.

Example 3 — Find Namespaces Declared on Children

The namespace is declared on a child element. Use the recursive option to find it.

PHP
<?php
$xml = '<root><x:item xmlns:x="urn:x">Tea</x:item></root>';
$root = simplexml_load_string($xml);

echo "Not recursive: ", count($root->getDocNamespaces()), PHP_EOL;
echo "Recursive: ", count($root->getDocNamespaces(true)), PHP_EOL;

How It Works

  1. The xmlns declaration sits on the item element, not on the root.
  2. By default only the root element is checked, so the result is empty.
  3. With true as the first argument, child elements are checked too.
  4. The recursive call finds the x namespace, so the count is 1.

Frequently Asked Questions

It returns an array of the namespaces declared in the document. Each key is a prefix and each value is a namespace URI.
getDocNamespaces() returns the namespaces that are declared, even if unused. getNamespaces() returns the namespaces that elements and attributes actually use.
An empty string. For xmlns="urn:d" the array contains an item with the key "" and the value urn:d.
Use it when namespaces are declared on child elements and not only on the root. Without it, only the starting element is checked.
When it is true, which is the default, the search starts at the document root even if you call the method on a child. Set it to false to start at the current element.

Next: SimpleXMLElement::getName()

SimpleXMLElement::getName() returns the name of an XML element or attribute.

SimpleXMLElement::getName() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful