note 92231 added to function.simplexml-element-xpath
| From: | GlennSchmidt at osu1 dot php dot net | Date: | Wed, 15 Jul 2009 07:31:40 +0000 |
| Subject: | note 92231 added to function.simplexml-element-xpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158320@lists.php.net to get a copy of this message | ||
Please note the counter-intuitive behaviour of this function. It will *always* search the entire
original document that you loaded, from the root level.
I would expect that if you are calling the xpath() method on a child element, the search would be
performed relative to that element, but this is not the case. In fact there seems to be no way to
restrict the search to the scope of a sub-element without knowing the full path to that sub-element,
and prepending it to the xpath string.
Example:
...
<library name="library1">
<book id="book1">...</book>
<book id="book2">...</book>
</library>
<library name="library2">
<book id="book1">...</book>
<book id="book2">...</book>
</library>
...
<?php
//Select the first library (library1)
$myLibrary = $xml->library[0];
//Search that library for a specific book
$myBook = $myLibrary->xpath('book[@id="book2"]'); //will return NO RESULTS
$myBook = $myLibrary->xpath('library[0]/book[@id="book2"]'); //will return
the book we want
$myBook = $myLibrary->xpath('//book[@id="book2"]'); //will return BOTH book2
records (not just the record from library1)
?>
----
Server IP: 117.55.229.1
Probable Submitter: 128.184.148.168
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-element-xpath.php
Edit -- https://master.php.net/note/edit/92231
Del: integrated -- https://master.php.net/note/delete/92231/integrated
Del: useless -- https://master.php.net/note/delete/92231/useless
Del: bad code -- https://master.php.net/note/delete/92231/bad+code
Del: spam -- https://master.php.net/note/delete/92231/spam
Del: non-english -- https://master.php.net/note/delete/92231/non-english
Del: in docs -- https://master.php.net/note/delete/92231/in+docs
Del: other reasons-- https://master.php.net/note/delete/92231
Reject -- https://master.php.net/note/reject/92231
Search -- https://master.php.net/manage/user-notes.php