PHP | SimpleXMLElement registerXPathNamespace() Function Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Pre-requisite: Read XML Basics The SimpleXMLElement::registerXPathNamespace() function is an inbuilt function in PHP which is used to create a namespace context for the XPath query to be executed next in a SimpleXML object. Syntax: bool SimpleXMLElement::registerXPathNamespace( $prefix, $namespace ) Parameter: This function accepts two parameters as mentioned above and described below: $prefix: It is required parameter. It is used in the XPath query for the namespace given in $namespace. $namespace:: It is required parameter. It specifies the namespace for the XPath query. Return Value: It returns True on success or False on Failure. Note: This function is available on PHP 5.2.0 and newer version. Below programs illustrate the SimpleXMLElement::registerXPathNamespace() function in PHP: Program 1: php <?php // Loading XML document to $user $user = <<<XML <user xmlns:user_id="https://www.geeksforgeeks.org/user"> <single_user id="1"> <user_id:id>12345</user_id:id> <username>Geeks123</username> <name>GeeksforGeeks</name> <phone>+91-XXXXXXXXXX</phone> <detail font-color="blue" font-size="24px"> Noida India </detail> </single_user> <single_user id="2"> <user_id:id>15980</user_id:id> <username>Geeks54321</username> <name>Geeks</name> <phone>+91-XXXXXXXXXX</phone> <detail font-color="blue" font-size="24px"> Noida India </detail> </single_user> </user> XML; // Loading string as simple xml object $xml = simplexml_load_string($user); // Registering Xpath namespace $xml->registerXPathNamespace('u', 'https://www.geeksforgeeks.org/user'); // Retrieving xpaths $result = $xml->xpath('//u:id'); // Printing output foreach ($result as $id) { echo $id . "<br>"; } ?> Output: 1234515980 Program 2: php <?php // Loading XML document to $user $user = <<<XML <user xmlns:user_id="https://www.geeksforgeeks.org/user"> <single_user id="1" xmlns:name="https://www.geeksforgeeks.org/user/name"> <user_id:id>12345</user_id:id> <username>rakesh123</username> <name:firstname>Rakesh</name:firstname> <name:lastname>Kumar</name:lastname> <phone>+91-XXXXXXXXXX</phone> <detail>Noida India</detail> </single_user> <single_user id="2" xmlns:name="https://www.geeksforgeeks.org/user/name"> <user_id:id>57833</user_id:id> <username>man123</username> <name:firstname>Manjeet</name:firstname> <name:lastname>Singh</name:lastname> <phone>+91-XXXXXXXXXX</phone> <detail>Kolkata, India</detail> </single_user> <single_user id="3" xmlns:name="https://www.geeksforgeeks.org/user/name"> <user_id:id>98944</user_id:id> <username>ak98</username> <name:firstname>Ak</name:firstname> <name:lastname>Singh</name:lastname> <phone>+91-XXXXXXXXXX</phone> <detail>Noida India</detail> </single_user> </user> XML; // Loading string as simple xml object $xml = simplexml_load_string($user); // Registering xpath namespace $xml->registerXPathNamespace('u', 'https://www.geeksforgeeks.org/user'); $xml->registerXPathNamespace('un', 'https://www.geeksforgeeks.org/user/name'); // Retrieving xpaths $result = $xml->xpath('//u:id'); $result_f_name = $xml->xpath('//un:firstname'); $result_l_name = $xml->xpath('//un:lastname'); // Displaying output foreach ($result as $id) { echo $id . "<br>"; } foreach ($result_f_name as $f_name) { echo $f_name . "<br>"; } foreach ($result_l_name as $l_name) { echo $l_name . "<br>"; } ?> Output: 123455783398944RakeshManjeetAkKumarSinghSingh Reference: https://www.php.net/manual/en/simplexmlelement.registerxpathnamespace.php Comment More infoAdvertise with us Next Article PHP | SimpleXMLElement getDocNamespaces() Function G gekcho Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | SimpleXMLElement getNamespaces() Function Pre-requisite: Read XML basics The SimpleXMLElement::getNamespaces() function is an inbuilt function in PHP which is used to retrieve the namespaces declared in XML document. Syntax: array SimpleXMLElement::getNamespaces( $recursive ) Parameter: This function accepts single parameter $recursive whic 2 min read PHP | SimpleXMLElement getDocNamespaces() Function Pre-requisite: Read XML Basics The SimpleXMLElement::getDocNamespaces() function is an inbuilt function in PHP which is used to retrieve the namespaces declared in XML document. Syntax: array SimpleXMLElement::getDocNamespaces( $recursive, $from_root ) Parameters: This function accepts two parameter 2 min read PHP | SimpleXMLElement saveXML() Function The SimpleXMLElement::saveXML() function is an inbuilt function in PHP which return well-formed XML string from a SimpleXML object. It is an alias of SimpleXMLElement::asXML() function. Syntax: mixed SimpleXMLElement::saveXML( string $filename ) Parameter: This function accepts single parameter $fil 2 min read PHP | SimpleXMLElement::getName() Function Pre-requisite: Read XML basicsThe SimpleXMLElement::getName() function is an inbuilt function in PHP which returns the name of the xml element.Syntax:Â Â string SimpleXMLElement::getName( void ) Parameter: This function does not accept any parameter.Return Value: It returns a string which represents 2 min read PHP | SimpleXMLElement asXML() Function Pre-requisite: Read XML The SimpleXMLElement::asXML() function is an inbuilt function in PHP which returns well-formed XML string from a SimpleXML object. Syntax: mixed SimpleXMLElement::asXML( $filename ) Parameters: This function accepts single parameter $filename which is optional. It specified t 2 min read PHP | SimpleXMLElement XPath() Function Pre-requisite: Read XML Basics The SimpleXMLElement::xpath() function is an inbuilt function in PHP which runs XPath query on the XML document. Syntax: SimpleXMLElement::xpath( $path ) Parameters: This function accepts single parameter $path which is required. It is used to specify the XPath path of 2 min read Like