note 44266 added to function.dom-domnodelist-item

From: Date: Tue, 20 Jul 2004 01:28:55 +0000
Subject: note 44266 added to function.dom-domnodelist-item
Groups: php.notes 
Request: Send a blank email to php-notes+get-73662@lists.php.net to get a copy of this message
The use of the count() function stumped me for a bit in this context, things can be a little confusing if you are using both SimpleXML and DOM XML (and are perhaps working late through the night :). In SimpleXML the result of an XPath expression returns an array of simplexml_element objects, so you can use count() to determine if the expression returned any nodes. In DOM XML the result of an XPath expression is a DOMNodeList which always returns 1 when it is passed into count, instead we can use DOMNodeList->length to get the number of nodes returned. As a simple example we have some session data stored using WDDX, the root node of any WDDX packet is wddxPacket, so when looking for wddxPacketBAD our XPath query should return 0 nodes. SimpleXML: $sessionDOM = simplexml_load_string($sessionXML) if($sessionDOM) { $xpQuery = $sessionDOM->xpath("//wddxPacketBAD"); print(count($xpQuery)); // prints 0 - OK } DOM XML: $sessionDOM = new DOMDocument(); $sessionDOM->loadXML($sessionXML); if($sessionDOM) { $xpHandle = new domXPath($sessionDOM); $xpQuery = $xpHandle->query("//wddxPacketBAD"); print(count($xpQuery)); // prints 1 - NOT OK print($xpQuery->length); // prints 0 - OK } Hope this helps! ---- Manual Page -- http://www.php.net/manual/en/function.dom-domnodelist-item.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44266 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44266&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44266&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#73662) next »