php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45201 Suggestion: allow SimpleXMLElement->xpath() to return single SimpleXMLElement
Submitted: 2008-06-07 14:09 UTC Modified: 2011-04-08 21:08 UTC
Votes:3
Avg. Score:2.3 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: php at jacob-head dot com Assigned:
Status: Open Package: SimpleXML related
PHP Version: 5.2.6 OS: Debian Testing (Lenny)
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-06-07 14:09 UTC] php at jacob-head dot com
Description:
------------
Currently SimpleXMLElement->xpath() returns an array comprised of SimpleXMLElements which match the xpath query. To manipulate the results it is therefore necessary to extract the elements from the array. This is not problematic for situations when one is using xpath to search for a number of nodes. However, when one searching for specific and unique elements, it would be helpful if rather than having xpath return an array comprised of the single element, it would be possible to have it return only the SimpleXMLElement object.

My suggestion is that a second parameter, an integer, be added to the function. If used, SimpleXMLElement->xpath() would return the SimpleXMLElement object equivalent to that result. For example:

$finalresult=$xml->xpath('element[@called="XYZ"]', 1);

would be the same as:

$firstresult=$xml->xpath('element[@called="XYZ"]');
$finalresult=$firstresult[0];

This would be useful as it would allow further manipulation of the element within a specific SimpleXML list of functions. For example:

$xml->xpath('element[@called="XYZ"]', 1)->addChild('new', 'element');

rather than the current:

$firstresult=$xml->xpath('element[@called="XYZ"]');
$firstresult[0]->addChild('new', 'element');

Equally, it would also allow stacking of xpath elements. E.g.:

$finalresult=$xml->xpath('element[@called="XYZ"]', 1)->xpath('child[@named="ABC"]', 1);

rather than the current:

$firstresult=$xml->xpath('element[@called="XYZ"]');
$secondresult=$firstresult[0]->xpath('child[@named="ABC"]');
$finalresult=$secondresult[0];

Although this may seem like a small change, when dealing with large numbers of these calls it would be very helpful.

Many thanks,

-Jacob

PS. This is my first attempt at a Wishlist Bug Report suggestion. I hope I have done everything correctly, but please accept my apologies if I have not!


Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-08 21:08 UTC] jani@php.net
-Package: Feature/Change Request +Package: SimpleXML related
 [2014-06-24 07:31 UTC] ahmad dot almajdi at yahoo dot dk
This is not a bug.
 [2016-01-07 12:34 UTC] contact at willianveiga dot com
I've created the "atXPath" method to return a single item, like Nokogiri does with at_xpath (http://www.nokogiri.org/tutorials/searching_a_xml_html_document.html).

The code is basically a copy from the "xpath" method with some modifications. Of course it must be improved and "DRYied".
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Fri Nov 16 11:01:25 2018 UTC