Example 1 — List the Declared Namespaces
Loop over the result to print each prefix and its URI.
<?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
- The root element declares one namespace with the prefix x.
- No element uses the prefix, but it is still declared.
- getDocNamespaces() returns an array with x as the key and urn:x as the value.
- foreach prints the prefix and the URI.

