|  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
Avg. Score:2.5 ± 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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: php at jacob-head dot com
New email:
PHP Version: OS:


 [2008-06-07 14:09 UTC] php at jacob-head dot com
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:


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[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:


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

Many thanks,


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!


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-08 21:08 UTC]
-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 (

The code is basically a copy from the "xpath" method with some modifications. Of course it must be improved and "DRYied".
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 09:01:27 2024 UTC