PHP SimpleXMLElement::getNamespaces() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::getNamespaces() returns the namespaces that are used by an element and its attributes.

The result is an array of prefix => URI pairs. By default only the current element is checked. Pass true to also check all child elements.

Syntax

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

PHP
$namespaces = $root->getNamespaces();
CallWhat it does
$root->getNamespaces()Returns the namespaces used by the current element and its attributes.
$root->getNamespaces(true)Also includes namespaces used by all child elements.
array_keys($root->getNamespaces(true))Gives only the list of prefixes.

This method lists the namespaces in use. A namespace that is declared but never used is not included. Use getDocNamespaces() to see declared ones.

Library declarations
PHP
SimpleXMLElement::getNamespaces(bool $recursive = false): array

Parameters

ParameterDescription
recursiveOptional. Set to true to also return namespaces used by child elements. The default is false.

Return Value

Return ValueDescription
arrayAn array of prefix => URI pairs. It is empty when no namespace is used.

PHP Examples

Example 1 — List the Namespaces in Use

The root element itself uses the x prefix, so it appears in the result.

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

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

How It Works

  1. The root element is x:root, so it uses the namespace urn:x.
  2. getNamespaces() returns an array with the prefix and URI.
  3. foreach goes through the pairs.
  4. Each line shows the prefix, an arrow, and the URI.

Example 2 — Use Recursive for Child Elements

Here only a child uses the namespace. Compare the result with and without the recursive option.

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

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

How It Works

  1. The root element has no namespace, and neither do its attributes.
  2. Without true, getNamespaces() checks only the root, so it finds nothing.
  3. With true, the child elements are checked as well.
  4. The item element uses x, so the count is 1.

Example 3 — Declared Is Not the Same as Used

Compare getNamespaces() with getDocNamespaces() for a namespace that is declared but never used.

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

echo "Used: ", count($root->getNamespaces(true)), PHP_EOL;
echo "Declared: ", count($root->getDocNamespaces()), PHP_EOL;

How It Works

  1. The root declares the prefix x with xmlns:x.
  2. No element or attribute uses that prefix.
  3. getNamespaces(true) looks for used namespaces, so it finds none.
  4. getDocNamespaces() reports the declaration, so its count is 1.

Frequently Asked Questions

It returns an array of the namespaces used by the element and its attributes. Each key is a prefix and each value is a URI.
getNamespaces() lists the namespaces that are used. getDocNamespaces() lists the namespaces that are declared, even if nothing uses them.
By default only the current element is checked. If the namespace is used on child elements, call getNamespaces(true).
An empty string. An element with xmlns="urn:d" gives an item with the key "" and the value urn:d.
Pass a URI or prefix from the result to children() or attributes() to read the elements and attributes in that namespace.

Next: SimpleXMLElement::registerXPathNamespace()

SimpleXMLElement::registerXPathNamespace() creates a prefix that you can use in XPath quer.

SimpleXMLElement::registerXPathNamespace() 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