• PHP Video Tutorials

PHP - SimpleXMLElement::registerXPathNamespace() Function



Definition and Usage

XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The SimpleXMLElement class represents an XML document in PHP.

The SimpleXMLElement::registerXPathNamespace() function is used to create a name space context for the XPath query.

Syntax

SimpleXMLElement::registerXPathNamespace($prefix, $namespace);

Parameters

Sr.No Parameter & Description
1

prefix (Mandatory)

This is a string value representing the namespace prefix.

2

namespace(Mandatory)

This is a string representing the namespace used for the XPath query.

Return Values

This function retuns a boolean value which is TRUE incase of success and FALSE incase of failure.

PHP Version

This function was first introduced in PHP Version 5 and works in all the later versions.

Example

Following example demonstrates the usage of the SimpleXMLElement::registerXPathNamespace() function.

<html>
   <head>
      <body>
      <?php
         $str = "<Data xmlns:ns='http://test.com/data'> 
         <Employee> 
            <ns:Name>Krishna</ns:Name> 
            <Age>30</Age> 
            <City>Hyderabad</City> 
         </Employee> 
  
         <Employee> 
            <ns:Name>Ramu</ns:Name>
            <Age>25</Age> 
            <City>Delhi</City> 
         </Employee>    
         </Data> "; 
         $xml = new SimpleXMLElement($str);
		 
         $xml->registerXPathNamespace('mynamespace', 'http://test.com/data'); 
  
         $res = $xml->xpath('//mynamespace:Name'); 
		 
         foreach ($res as $node) {
            print($node . "<br>");
         }
      ?>      
      </body>
   </head>   
</html> 

This will produce following result −

Krishna
Ramu
php_function_reference.htm
Advertisements