PHP | DOMXPath __construct() Function Last Updated : 17 Mar, 2020 Comments Improve Suggest changes Like Article Like Report The DOMXPath::__construct() function is an inbuilt function in PHP which is used to create an instance of DOMXPath. Syntax: bool DOMXPath::__construct( DOMDocument $doc ) Parameters: This function accepts a single parameter $doc which holds the DOMDocument associated with the DOMXPath. Below examples illustrate the DOMXPath::__construct() function in PHP: Example 1: php <?php // Create a new DOMDocument instance $document = new DOMDocument(); // Create a XML $xml = <<<XML <?xml version="1.0" encoding="utf-8"?> <content> Hello World </content> XML; // Load the XML $document->loadXML($xml); // Create a new DOMXPath instance $xpath = new DOMXPath($document); // Get the element $tbody = $document-> getElementsByTagName('content')->item(0); // Get the element with name content $query = '//content'; // Evaluate the query $entries = $xpath->evaluate($query, $tbody); echo $entries[0]->nodeValue; ?> Output: Hello World Example 2: 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); // Count the number of element with // name content $query = 'count(//content)'; // Evaluate the query $entries = $xpath->evaluate($query, $tbody); echo $entries; ?> Output: 3 Reference: https://www.php.net/manual/en/domxpath.construct.php Comment More infoAdvertise with us Next Article PHP | DOMXPath __construct() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMAttr __construct() Function The DOMAttr::__construct() function is an inbuilt function in PHP which is used to create a new DOMAttr object. This created object is a read-only type. Syntax: public DOMAttr::__construct( string $name, string $value ) Parameters: This function accepts two parameters as mentioned above and describe 2 min read PHP | DOMElement __construct() Function The DOMElement::__construct() function is an inbuilt function in PHP which is used to create a new DOMElement object. This object is read-only and may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. Syntax: public DOMElem 2 min read PHP | DOMComment __construct() Function The DOMComment::__construct() function is an inbuilt function in PHP which creates a new DOMComment object. This object is read only and can be appended to a document Syntax: public DOMComment::__construct( string $value) Parameters: This function accepts a single parameter $value which holds the co 1 min read PHP | DOMDocument __construct() Function The DOMDocument::__construct() function is an inbuilt function in PHP which is used to create a new DOMDocument object. Syntax: public DOMDocument::__construct( string $version, string $encoding ) Parameters: This function accepts two parameters as mentioned above and described below: $version: This 1 min read PHP | DOMImplementation __construct() Function The DOMImplementation::__construct() function is an inbuilt function in PHP which is used to create a new DOMImplementation object. Syntax: DOMImplementation::__construct( void ) Parameters: This function doesnât accept any parameter. Below examples illustrate the DOMImplementation::__construct() fu 1 min read Like