PHP | DOMXPath query() Function Last Updated : 13 Mar, 2020 Comments Improve Suggest changes Like Article Like Report 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. php <?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>"; } ?> Output: First Second Third Program 2: In this program we will count all the elements with name h1. php <?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); ?> Output: 4 Reference: https://www.php.net/manual/en/domxpath.query.php Comment More infoAdvertise with us Next Article PHP | DOMXPath query() Function gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads 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 abov 2 min read PHP | http_build_query() Function The http_build_query() function is an inbuilt function in PHP which is used to generate URL-encoded query string from the associative (or indexed) array. Syntax: string http_build_query( $query_data, $numeric_prefix, $arg_separator, $enc_type = PHP_QUERY_RFC1738 ) Parameters: This function accepts f 2 min read PHP DOMXPath registerPhpFunctions() Function The DOMXPath::registerPhpFunctions() function is an inbuilt function in PHP which is used to enable the ability to use PHP functions within XPath expressions. Syntax:void DOMXPath::registerPhpFunctions( mixed $restrict )Parameters: This function accepts an optional single parameter $restrict which h 2 min read PHP | DOMAttr isId() Function The DOMAttr::isId() function is an inbuilt function in PHP which is used to check if an attribute is a defined ID or not. According to the DOM standard, it requires the attribute ID to be of type ID. You need to validate your document with DOMDocument::validateOnParse() method before using this func 2 min read PHP fpassthru( ) Function The fpassthru() function in PHP is an inbuilt function which is used to read data from a current position from a specified file until end of file and then write the result to the output buffer. The file which has to be read is sent as a parameter to the fpassthru() function and it returns the number 2 min read PHP | DOMDocument save() Function The DOMDocument::save() function is an inbuilt function in PHP which is used to create an XML document from the DOM representation. This function is used after creating the new dom document from scratch. Syntax: int DOMDocument::save( string $filename, int $options = 0 ) Parameters: This function ac 2 min read PHP | DOMDocument xinclude() Function The DOMDocument::xinclude() function is an inbuilt function in PHP which is used to substitute the XIncludes in a DOMDocument Object. Syntax: int DOMDocument::xinclude( int $options = 0 ) Parameters: This function accepts an optional single parameter $options which holds the libxml parameter. Return 2 min read PHP | XMLWriter endComment() Function The XMLWriter::endComment() function is an inbuilt function in PHP which is used to end a comment which is started using XMLWriter::startComment() function. Syntax: bool XMLWriter::endComment( void ) Parameters: This function doesnât accept any parameter. Return Value: This function returns TRUE on 1 min read PHP XMLWriter endCdata() Function The XMLWriter::endCdata() function is an inbuilt function in PHP which is used to end current CDATA. CDATA is block of text which is not parsed by the parser but are recognized as markup. Syntax: bool XMLWriter::endCdata( void ) Parameters: This function doesnât accept any parameter. Return Value: 2 min read PHP | XMLWriter endDocument() Function The XMLWriter::endDocument() function is an inbuilt function in PHP which is used to end current document. Syntax: bool XMLWriter::endDocument( void ) Parameters: This function doesnât accept any parameter. Return Value: This function returns TRUE on success or FALSE on failure. Below examples illus 1 min read Like