PHP | DOMXPath registerNamespace() Function
The DOMXPath::registerNamespace() function is an inbuilt function in PHP which is used to register the namespaceURI and prefix with the DOMXPath object.
Syntax:
bool DOMXPath::registerNamespace( string $prefix,
string $namespaceURI )
Parameters: This function accepts two parameters as mentioned above and described below:
- $prefix: It specifies the prefix.
- $namespaceURI: It specifies the namespace URI.
Return Value: This function returns TRUE on success or FALSE on failure.
Below given programs illustrate the DOMXPath::registerNamespace() function in PHP:
Program 1:
<?php
$document = new DOMDocument();
$xml = <<<XML
<?xml version= "1.0" encoding= "UTF-8" ?>
<body xmlns= "geeksforgeeks" >
<h1>Hello</h1>
</body>
XML;
$document ->loadXML( $xml );
$xpath = new DOMXPath( $document );
$xpath ->registerNamespace( 'x' ,
'geeksforgeeksnew' );
$query = '//x:body/x:h1/text()' ;
$entries = $xpath ->evaluate( $query );
echo count ( $entries ) . "\n" ;
?>
|
Output:
0
Program 2:
<?php
$document = new DOMDocument();
$xml = <<<XML
<?xml version= "1.0" encoding= "UTF-8" ?>
<body xmlns= "geeksforgeeks" >
<h1>Hello</h1>
</body>
XML;
$document ->loadXML( $xml );
$xpath = new DOMXPath( $document );
$xpath ->registerNamespace( 'x' ,
'geeksforgeeks' );
$query = '//x:body/x:h1/text()' ;
$entries = $xpath ->evaluate( $query );
echo $entries ->item(0)->nodeValue . "\n" ;
?>
|
Output:
Hello
Reference: https://www.php.net/manual/en/domxpath.registernamespace.php
Last Updated :
26 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...