note 95553 deleted from domxpath.query by salathe

From: Date: Wed, 11 Aug 2010 08:43:41 +0000
Subject: note 95553 deleted from domxpath.query by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171563@lists.php.net to get a copy of this message
Note Submitter: kkez ---- If the query() function seems to ignore your $contextnode, and instead returns all the tags in the document, try to use a relative path (use a . in front of the query): <?php $xml = "<?xml version='1.0' encoding='UTF-8'?> <test> <tag1> <uselesstag> <tag2>test</tag2> </uselesstag> </tag1> <tag2>test2</tag2> </test>"; $dom = new DomDocument(); $dom->loadXML($xml); $xpath = new DomXPath($dom); $tag1 = $dom->getElementsByTagName("tag1")->item(0); echo $xpath->query("//tag2")->length; //output 2 -> correct echo $xpath->query("//tag2", $tag1);->length; //output 2 -> wrong, the query is not relative echo $xpath->query(".//tag2", $tag1);->length; //output 1 -> correct (note the dot in front of //) ?> I thought PHP would do this for me, but it doesn't.

« previous php.notes (#171563) next »