note 92231 deleted from function.simplexml-element-xpath by danbrown

From: Date: Wed, 16 Sep 2009 15:49:54 +0000
Subject: note 92231 deleted from function.simplexml-element-xpath by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160670@lists.php.net to get a copy of this message
Note Submitter: Glenn Schmidt ---- 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) ?>

« previous php.notes (#160670) next »