Req #45201 [Com]: Suggestion: allow SimpleXMLElement->xpath() to return single SimpleXMLElement
| From: | contact at willianveiga dot com | Date: | Thu, 07 Jan 2016 12:34:55 +0000 |
| Subject: | Req #45201 [Com]: Suggestion: allow SimpleXMLElement->xpath() to return single SimpleXMLElement | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-198489@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=45201&edit=1
ID: 45201
Comment by: contact at willianveiga dot com
Reported by: php at jacob-head dot com
Summary: Suggestion: allow SimpleXMLElement->xpath() to
return single SimpleXMLElement
Status: Open
Type: Feature/Change Request
Package: SimpleXML related
Operating System: Debian Testing (Lenny)
PHP Version: 5.2.6
Block user comment: N
Private report: N
New Comment:
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".
Previous Comments:
------------------------------------------------------------------------
[2014-06-24 07:31:31] ahmad dot almajdi at yahoo dot dk
This is not a bug.
------------------------------------------------------------------------
[2008-06-07 14:09:47] 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!
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=45201&edit=1