note 84577 added to domxpath.query

From: Date: Sun, 20 Jul 2008 04:01:18 +0000
Subject: note 84577 added to domxpath.query
Groups: php.notes 
Request: Send a blank email to php-notes+get-142103@lists.php.net to get a copy of this message
If you're wondering, like I was, why your XPath queries are not returning any of the new DOMElements you create in your (X)HTML documents, and only the ones originally loaded in with (for example) loadXML(), this is why; if you're doing things right, you have registered the nameSpace 'html' after creating your DOMXPath object thus: <?php class XPathQueryLength { private $nameSpace = ''; function __construct(DOMDocument $doc) { $this->xpath = new DOMXPath($this->doc); $this->xpath->registerNamespace( 'html','http://www.w3.org/1999/xhtml' ); } function queryLength($query) { return $this->xpath->query($query)->length; } } ?> ...but don't forget that when adding new elements to the above DOMDocument $doc, to use createElementNS() instead of createElement(), otherwise you'll have this problem: <?php //$doc is a previously loaded XHTML document containing a normal html, head and body structure //$body is the first selected tag using $doc->getElementsByTagName('body'); $pTag = $doc->createElement('p','This is a new paragraph!'); $body->appendChild($pTag); $xPath = new XPathQueryLength($doc); print $xPath->queryLength('//html:p'); output: 0 print $xPath->queryLength('//p'); output: 1 ?> So do this instead: <?php //$doc is a previously loaded XHTML document containing a normal html, head and body structure //$body is the first selected tag using $doc->getElementsByTagName('body'); $pTag = $doc->createElementNS('http://www.w3.org/1999/xhtml','p','This is a new paragraph!'); $body->appendChild($pTag); $xPath = new XPathQueryLength($doc); print $xPath->queryLength('//html:p'); output: 2 print $xPath->queryLength('//p'); output: 0 ?> The resulting XHTML file from both example scripts looks much like this: <html> <head></head> <body> <p>This is a hardcoded paragraph.</p> <p>This is a new paragraph!</p> </body> </html> ...so you would think that a paragraph is a paragraph is a paragraph, since you never see the prefix, as in "<html:p>This is a new paragraph!</html:p>". This may seem glaringly obvious, but I was writing a class that converts CSS queries into XPath queries, and the fact that a namespace had been registered was rather buried in the code. We love the DOM, the DOM is good to us. ---- Server IP: 64.71.164.2 Probable Submitter: 70.95.96.201 ---- Manual Page -- http://www.php.net/manual/en/domxpath.query.php Edit -- https://master.php.net/note/edit/84577 Del: integrated -- https://master.php.net/note/delete/84577/integrated Del: useless -- https://master.php.net/note/delete/84577/useless Del: bad code -- https://master.php.net/note/delete/84577/bad+code Del: spam -- https://master.php.net/note/delete/84577/spam Del: non-english -- https://master.php.net/note/delete/84577/non-english Del: in docs -- https://master.php.net/note/delete/84577/in+docs Del: other reasons-- https://master.php.net/note/delete/84577 Reject -- https://master.php.net/note/reject/84577 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#142103) next »