PHP | DOMXPath evaluate() Function

The DOMXPath::evaluate() function is an inbuilt function in PHP which is used to execute the given XPath expression which is a pattern defined to select a set of nodes.

Syntax:

mixed DOMXPath::evaluate( string $expression, 
          DOMNode $contextnode, bool $registerNodeNS )

Parameters: This function accept three parameters as mentioned above and described below:



  • $expression: It specifies the XPath expression to execute.
  • $contextnode (Optional): It specifies the contextnode for doing relative XPath queries. By default, the queries are relative to the root element.
  • $registerNodeNS (Optional): It specifies whether to disable automatic registration of the context node.

Return Value: This function returns TRUE on success.

Exceptions: This function throws DOMXPathException on error.

Below examles illustrate the DOMXPath::evaluate() function in PHP:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create a new DOMDocument instance
$document = new DOMDocument();
   
// Create a XML
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book>
  <title lang="en">GeeksforGeeks</title>
  <price>400</price>
</book>
<book>
  <title lang="en">Intro to XML</title>
  <price>300</price>
</book>
</bookstore>
XML;
   
// Load the XML
$document->loadXML($xml);
   
// Create a new DOMXPath instance
$xpath = new DOMXPath($document);
   
// Get the
$tbody = $document->
    getElementsByTagName('bookstore')->item(0);
   
// Query to get the number of titles with lang 
// attribute "en"
$query = 'count(//title[@lang=\'en\'])';
   
// Evaluate the query
$entries = $xpath->evaluate($query, $tbody);
echo "Number of elements with lang = \"en\": $entries\n";
?>

chevron_right


Output:

Number of elements with lang = "en": 2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Create a XML
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<GeeksforGeeks>
  Keep Learning.
</GeeksforGeeks>
XML;
  
// Load the XML
$document->loadXML($xml);
  
// Create a new DOMXPath instance
$xpath = new DOMXPath($document);
  
// Get the
$tbody = $document->
getElementsByTagName('GeeksforGeeks')->item(0);
  
// Get the element with name GeeksforGeeks
$query = '//GeeksforGeeks';
  
// Evaluate the query
$entries = $xpath->evaluate($query, $tbody);
echo $entries[0]->nodeValue;
?>

chevron_right


Output:

Keep Learning.

Reference: https://www.php.net/manual/en/domxpath.evaluate.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.