note 95553 added to domxpath.query
| From: | kkez at osu1 dot php dot net | Date: | Fri, 08 Jan 2010 16:10:02 +0000 |
| Subject: | note 95553 added to domxpath.query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164791@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 85.18.24.54
----
Manual Page -- http://www.php.net/manual/en/domxpath.query.php
Edit -- https://master.php.net/note/edit/95553
Del: integrated -- https://master.php.net/note/delete/95553/integrated
Del: useless -- https://master.php.net/note/delete/95553/useless
Del: bad code -- https://master.php.net/note/delete/95553/bad+code
Del: spam -- https://master.php.net/note/delete/95553/spam
Del: non-english -- https://master.php.net/note/delete/95553/non-english
Del: in docs -- https://master.php.net/note/delete/95553/in+docs
Del: other reasons-- https://master.php.net/note/delete/95553
Reject -- https://master.php.net/note/reject/95553
Search -- https://master.php.net/manage/user-notes.php