Example 1 — List the Namespaces in Use
The root element itself uses the x prefix, so it appears in the result.
<?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
- The root element is x:root, so it uses the namespace urn:x.
- getNamespaces() returns an array with the prefix and URI.
- foreach goes through the pairs.
- Each line shows the prefix, an arrow, and the URI.

