Example 1 — Query a Namespaced Element
Register a prefix for the namespace and use it in an XPath query.
<?php
$xml = '<root xmlns:x="urn:x"><x:item>Tea</x:item></root>';
$root = simplexml_load_string($xml);
var_dump($root->registerXPathNamespace('p', 'urn:x'));
$items = $root->xpath('//p:item');
echo (string) $items[0], PHP_EOL; How It Works
- The item element is in the namespace urn:x.
- registerXPathNamespace() links the prefix p to that namespace and returns true.
- xpath('//p:item') uses the new prefix to find the element.
- xpath() returns an array, so $items[0] is cast to a string to print Tea.

