PHP SimpleXMLElement::registerXPathNamespace() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::registerXPathNamespace() creates a prefix that you can use in XPath queries for a namespace.

XPath cannot match elements in a namespace without a prefix, even for the default namespace. You register a prefix of your own, and then use it in xpath(). The prefix does not have to match the one in the document.

Syntax

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

PHP
$root->registerXPathNamespace('p', 'urn:x');
CallWhat it does
$root->registerXPathNamespace('p', 'urn:x')Lets you write p:item in later xpath() calls on $root.
$root->xpath('//p:item')Uses the registered prefix to find the elements.
var_dump($root->registerXPathNamespace('p', 'urn:x'))Shows the return value, which is true when registration worked.

The prefix is stored on the object you call it on. Register it again on any other SimpleXMLElement object that you want to query.

Library declarations
PHP
SimpleXMLElement::registerXPathNamespace(string $prefix, string $namespace): bool

Parameters

ParameterDescription
prefixRequired. The prefix to use inside XPath expressions.
namespaceRequired. The namespace URI that the prefix stands for.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Query a Namespaced Element

Register a prefix for the namespace and use it in an XPath query.

PHP
<?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

  1. The item element is in the namespace urn:x.
  2. registerXPathNamespace() links the prefix p to that namespace and returns true.
  3. xpath('//p:item') uses the new prefix to find the element.
  4. xpath() returns an array, so $items[0] is cast to a string to print Tea.

Example 2 — The Default Namespace Needs a Prefix

Without a registered prefix, XPath cannot see the elements in a default namespace.

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

echo "Without prefix: ", count($root->xpath('//item')), PHP_EOL;

$root->registerXPathNamespace('d', 'urn:d');

echo "With prefix: ", count($root->xpath('//d:item')), PHP_EOL;

How It Works

  1. The root declares a default namespace, so item belongs to urn:d.
  2. The plain query //item looks for item with no namespace and finds nothing.
  3. registerXPathNamespace() links the prefix d to urn:d.
  4. //d:item now matches the element, so the count is 1.

Example 3 — Loop Over XPath Results

Register a prefix once, then run a path query and loop over every match.

PHP
<?php
$xml = '<feed xmlns="urn:feed"><entry>A</entry><entry>B</entry></feed>';
$feed = simplexml_load_string($xml);

$feed->registerXPathNamespace('f', 'urn:feed');

foreach ($feed->xpath('/f:feed/f:entry') as $entry) {
    echo (string) $entry, PHP_EOL;
}

How It Works

  1. The feed and its entries are all in the default namespace urn:feed.
  2. The prefix f is registered for that namespace.
  3. The XPath path /f:feed/f:entry needs the prefix on every step.
  4. foreach prints the text of each matching entry.

Frequently Asked Questions

It registers a prefix for a namespace URI, so you can use that prefix in XPath queries made with xpath().
The elements are probably in a namespace, often the default one. Register a prefix with registerXPathNamespace() and use it in the query.
No. Only the URI must match. You can choose any prefix, such as p or d.
Yes. The prefix is stored on the object where you registered it. Register it again on any other object you query.
It returns true when the prefix was registered and false on failure.

Next: SimpleXMLElement::saveXML()

SimpleXMLElement::saveXML() turns a SimpleXML object back into an XML string, or writes th.

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