PHP | DOMXPath query() Function

The DOMXPath::query() function is an inbuilt function in PHP which is used to evaluate the given XPath expression.

Syntax:

DOMNodeList DOMXPath::query( 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 optional contextnode for doing relative XPath queries. By default, the queries are relative to the root element.
  • $registerNodeNS (Optional): It specifies the optional registerNodeNS to disable automatic registration of the context node.

Return Value: This function returns a DOMNodeList containing all nodes matching the given XPath expression. Any expression which does not return nodes will return an empty DOMNodeList.

Below given programs illustrate the DOMXPath::query() function in PHP:

Program 1: In this program we will fetch all the element values of elements with name content.

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"?>
<root>
    <content>
        First
    </content>
    <content>
        Second
    </content>
    <content>
        Third
    </content>
</root>
XML;
  
// Load the XML
$document->loadXML($xml);
  
// Create a new DOMXPath instance
$xpath = new DOMXPath($document);
  
// Get the root element
$tbody = $document->getElementsByTagName('root')->item(0);
  
// Get all the element with name content
$query = '//content';
  
// Execute the query
$entries = $xpath->query($query);
  
foreach ($entries as $entry) {
    echo $entry->nodeValue . "<br>";
}
?>

chevron_right


Output:

First
Second
Third

Program 2: In this program we will count all the elements with name h1.

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"?>
<root>
    <h1>
        Hello
    </h1>
    <h1>
        World
    </h1>
    <h1>
        Foo
    </h1>
    <h1>
        Bar
    </h1>
</root>
XML;
  
// Load the XML
$document->loadXML($xml);
  
// Create a new DOMXPath instance
$xpath = new DOMXPath($document);
  
// Get the root element
$tbody = $document->getElementsByTagName('root')->item(0);
  
// Get all the element with name h1
$query = '//h1';
  
// Execute the query
$entries = $xpath->query($query);
  
// Count the number of headings
echo count($entries);
?>

chevron_right


Output:

4

Reference: https://www.php.net/manual/en/domxpath.query.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.