Req #45201 [Com]: Suggestion: allow SimpleXMLElement->xpath() to return single SimpleXMLElement

From: Date: Tue, 24 Jun 2014 07:31:32 +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-186322@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:         ahmad dot almajdi at yahoo dot dk
 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:

This is not a bug.


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


Thread (3 messages)

« previous php.bugs (#186322) next »