note 130892 added to domxpath.query

From: Date: Tue, 22 Sep 2026 15:35:08 +0000
Subject: note 130892 added to domxpath.query
Groups: php.notes 
Request: Send a blank email to php-notes+get-222645@lists.php.net to get a copy of this message
Bug or feature, but if you don't specify the $contextNode, it does not default to the DOMDocument object, but instead its $domDocument->documentElement (which is the <html> element in case of a HTML document). <?php function print_nodes( $nodes ){ foreach( $nodes as $node ) print_node( $node ); } function print_node( $node ){ echo $node->nodeName, ' ', getNodeTypeName( $node->nodeType ), ' ', $node->getNodePath(), "\n"; } function getNodeTypeName( $nodeType ){ foreach( get_defined_constants() as $k => $v ) if( $v === $nodeType && preg_match('#XML_(.+)_NODE#', $k, $m )) return $m[1]; } $doc = new DOMDocument(); $doc->loadHTML('<html></html>'); // or loadXML() $domxpath = new DOMXPath( $doc ); // The document and the <html> element: print_node( $doc ); // #document HTML_DOCUMENT / print_node( $doc->documentElement ); // html ELEMENT /html // Root node accessed by an absolute path is the document: print_nodes( $domxpath->query('/')); // #document HTML_DOCUMENT / // Passing document as $contextNode makes it the actual context node: print_nodes( $domxpath->query('.', $doc )); // #document HTML_DOCUMENT / // But omitting the $contextNode makes <html> the actual context node: print_nodes( $domxpath->query('.')); // html ELEMENT /html ?> ---- Server IP: 2a03:b0c0:2:f0:0:1:a983:a001 (proxied: BunnyCDN) Probable Submitter: 62.165.224.36 ---- Manual Page -- https://php.net/manual/en/domxpath.query.php Edit -- https://main.php.net/note/edit/130892 Del: integrated -- https://main.php.net/note/delete/130892/integrated Del: useless -- https://main.php.net/note/delete/130892/useless Del: bad code -- https://main.php.net/note/delete/130892/bad+code Del: spam -- https://main.php.net/note/delete/130892/spam Del: non-english -- https://main.php.net/note/delete/130892/non-english Del: in docs -- https://main.php.net/note/delete/130892/in+docs Del: other reasons-- https://main.php.net/note/delete/130892 Reject -- https://main.php.net/note/reject/130892 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222645) next »