Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] SimpleXML's atXPath method

Posted by Willian Veiga 
Willian Veiga
[PHP-DEV] SimpleXML's atXPath method
January 06, 2017 03:10PM
Hello,

What do you think about a new method, called atXPath, that would return a
single SimpleXMLElement?

Details:

https://github.com/php/php-src/pull/1717
https://bugs.php.net/bug.php?id=45201

Thank you, Willian.
Rowan Collins
Re: [PHP-DEV] SimpleXML's atXPath method
January 07, 2017 05:30PM
On 06/01/2017 14:03, Willian Veiga wrote:
> Hello,
>
> What do you think about a new method, called atXPath, that would return a
> single SimpleXMLElement?
>
> Details:
>
> https://github.com/php/php-src/pull/1717
> https://bugs.php.net/bug.php?id=45201
>
> Thank you, Willian.
>

My first thought on this was "what does the 'at' mean?" I see now that
it's copied from another library, but "atXPath" really doesn't make me
think "xpath for single element".

My second thought is that this feature is actually no longer needed. The
original feature request was to avoid the need for an intermediate
variable when you know you're getting back one result:

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

But in all versions of PHP since 5.4 you can simply add the [0] on the
end of the function call anyway:

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

Like the original proposal for an extra parameter, this extends to
selecting whichever result you want, as long as you're sure it exists.

Example: https://3v4l.org/1k9rk

If you don't know for sure that there will be enough results, then
chaining will give an error either way, and this isn't really any
different from any other method that might or might not return the
expected object.

Regards,

--
Rowan Collins
[IMSoP]


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Joe Watkins
Re: [PHP-DEV] SimpleXML's atXPath method
January 07, 2017 06:10PM
Evening Rowan,

I must protest: It is wasteful, unclear, and generally nasty, to create,
return, and manipulate an array if you are only ever going to reference one
element in code.

It is quite normal to know the structure of a document ahead of time, and
this would allow you to write more succinct and less wasteful code.

It would appear valuable to me ... I'm not in love with the name of the
thing, but the basic idea appears valuable.

Cheers
Joe

On Sat, Jan 7, 2017 at 4:26 PM, Rowan Collins <[email protected]>
wrote:

> On 06/01/2017 14:03, Willian Veiga wrote:
>
>> Hello,
>>
>> What do you think about a new method, called atXPath, that would return a
>> single SimpleXMLElement?
>>
>> Details:
>>
>> https://github.com/php/php-src/pull/1717
>> https://bugs.php.net/bug.php?id=45201
>>
>> Thank you, Willian.
>>
>>
> My first thought on this was "what does the 'at' mean?" I see now that
> it's copied from another library, but "atXPath" really doesn't make me
> think "xpath for single element".
>
> My second thought is that this feature is actually no longer needed. The
> original feature request was to avoid the need for an intermediate variable
> when you know you're getting back one result:
>
> $firstresult=$xml->xpath('element[@called="XYZ"]');
> $secondresult=$firstresult[0]->xpath('child[@named="ABC"]');
> $finalresult=$secondresult[0];
>
> But in all versions of PHP since 5.4 you can simply add the [0] on the end
> of the function call anyway:
>
> $finalresult=$xml->xpath('element[@called="XYZ"]')[0]->xpath
> ('child[@named="ABC"]')[0];
>
> Like the original proposal for an extra parameter, this extends to
> selecting whichever result you want, as long as you're sure it exists.
>
> Example: https://3v4l.org/1k9rk
>
> If you don't know for sure that there will be enough results, then
> chaining will give an error either way, and this isn't really any different
> from any other method that might or might not return the expected object.
>
> Regards,
>
> --
> Rowan Collins
> [IMSoP]
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Kalle Sommer Nielsen
Re: [PHP-DEV] SimpleXML's atXPath method
January 07, 2017 06:40PM
2017-01-07 18:01 GMT+01:00 Joe Watkins <[email protected]>:
> Evening Rowan,
>
> I must protest: It is wasteful, unclear, and generally nasty, to create,
> return, and manipulate an array if you are only ever going to reference one
> element in code.
>
> It is quite normal to know the structure of a document ahead of time, and
> this would allow you to write more succinct and less wasteful code.
>
> It would appear valuable to me ... I'm not in love with the name of the
> thing, but the basic idea appears valuable.

I agree, the feature/idea is great. Coming from someone who worked a
fair bit with XML recently (I know I know!). Same goes for the method
naming, seems a little bit awkward

--
regards,

Kalle Sommer Nielsen
kalle@php.net

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Rowan Collins
Re: [PHP-DEV] SimpleXML's atXPath method
January 07, 2017 07:30PM
On 07/01/2017 17:01, Joe Watkins wrote:
> Evening Rowan,
>
> I must protest: It is wasteful, unclear, and generally nasty, to
> create, return, and manipulate an array if you are only ever going to
> reference one element in code.

Meh, feels like a micro-optimisation to me, and "[0] to access first
search result" seems pretty obvious, but fair enough if you disagree.

I know development's not a zero-sum game, but there are plenty of things
I'd add to the SimpleXML extension that aren't just aliases for things
you can trivially do already.

For instance, sticking with XPath usage, the fact that
registerXPathNamespace() only affects the current element, so has to be
re-run as you traverse:

$sx->registerXPathNamespace('a', 'http://example.com');
$foo = $xml->xpath('//a:foo')[0];
// do something with $foo
$foo->registerXPathNamespace('a', 'http://example.com'); // won't work
without this
$bar = $foo->xpath('./a:bar')[0];

That's a lot more intrusive than needing to add "[0]" next to a few
method calls.

Regards,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Joe Watkins
Re: [PHP-DEV] SimpleXML's atXPath method
January 07, 2017 08:40PM
Evening Roawn,

There is no competition to see which single problem we are going to solve,
we can fix more than one thing, obviously :)

What we are discussing here is the value of this particular feature, not
the value of things that nobody has taken the time to propose, implement,
or fix.

This contributor has waited a year for this conversation to start, it would
be nice if we could stay focused.

If you would like to talk about the shortcomings in simplexml (or
anything), might I suggest you start a new topic and see if you can drum up
some support for changes/fixes :)

Cheers
Joe

On Sat, Jan 7, 2017 at 6:24 PM, Rowan Collins <[email protected]>
wrote:

> On 07/01/2017 17:01, Joe Watkins wrote:
>
>> Evening Rowan,
>>
>> I must protest: It is wasteful, unclear, and generally nasty, to create,
>> return, and manipulate an array if you are only ever going to reference one
>> element in code.
>>
>
> Meh, feels like a micro-optimisation to me, and "[0] to access first
> search result" seems pretty obvious, but fair enough if you disagree.
>
> I know development's not a zero-sum game, but there are plenty of things
> I'd add to the SimpleXML extension that aren't just aliases for things you
> can trivially do already.
>
> For instance, sticking with XPath usage, the fact that
> registerXPathNamespace() only affects the current element, so has to be
> re-run as you traverse:
>
> $sx->registerXPathNamespace('a', 'http://example.com');
> $foo = $xml->xpath('//a:foo')[0];
> // do something with $foo
> $foo->registerXPathNamespace('a', 'http://example.com'); // won't work
> without this
> $bar = $foo->xpath('./a:bar')[0];
>
> That's a lot more intrusive than needing to add "[0]" next to a few method
> calls.
>
>
> Regards,
> --
> Rowan Collins
> [IMSoP]
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Rowan Collins
Re: [PHP-DEV] SimpleXML's atXPath method
January 08, 2017 12:10AM
On 07/01/2017 19:29, Joe Watkins wrote:
> Evening Roawn,
>
> There is no competition to see which single problem we are going to
> solve, we can fix more than one thing, obviously :)
>
> What we are discussing here is the value of this particular feature,
> not the value of things that nobody has taken the time to propose,
> implement, or fix.
>
> This contributor has waited a year for this conversation to start, it
> would be nice if we could stay focused.
>

Indeed, I didn't mean to imply otherwise, and sorry if I gave that
impression. There is a small cost to adding new methods like this, in
future maintenance, and the future consistency of the extension, but
you're right that it's not a big deal, and other than finding the right
name, I've no real reason to object to this method.

Regards,

--
Rowan Collins
[IMSoP]


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Joe Watkins
Re: [PHP-DEV] SimpleXML's atXPath method
January 08, 2017 12:30AM
Evening Rowan,

Thank you.

Now we can get to the really hard problem ... naming things :D

I'm afraid I have no good suggestions. I can find API's that use the
suggested name, but they vary in functionality (some overlap).

There's also XmlNode.SelectSingleNode from C#, but sounds pretty horrible
to me, variations on it might be:

selectPath
singlePath
singleNode

None of them really sound right to me, but may provoke someone else to do
better.

Over to you, someone else ...

Cheers
Joe

On Sat, Jan 7, 2017 at 11:02 PM, Rowan Collins <[email protected]>
wrote:

> On 07/01/2017 19:29, Joe Watkins wrote:
>
>> Evening Roawn,
>>
>> There is no competition to see which single problem we are going to
>> solve, we can fix more than one thing, obviously :)
>>
>> What we are discussing here is the value of this particular feature, not
>> the value of things that nobody has taken the time to propose, implement,
>> or fix.
>>
>> This contributor has waited a year for this conversation to start, it
>> would be nice if we could stay focused.
>>
>>
> Indeed, I didn't mean to imply otherwise, and sorry if I gave that
> impression. There is a small cost to adding new methods like this, in
> future maintenance, and the future consistency of the extension, but you're
> right that it's not a big deal, and other than finding the right name, I've
> no real reason to object to this method.
>
>
> Regards,
>
> --
> Rowan Collins
> [IMSoP]
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Stephen Reay
Re: [PHP-DEV] SimpleXML's atXPath method
January 08, 2017 02:50AM
Hey all,

Is there a major issue with simply adding a $limit parameter to the existing xpath() method instead, and stopping searching after that many elements are found?


Cheers

Stephen

Sent from my iPhone

> On 8 Jan 2017, at 06:19, Joe Watkins <[email protected]> wrote:
>
> Evening Rowan,
>
> Thank you.
>
> Now we can get to the really hard problem ... naming things :D
>
> I'm afraid I have no good suggestions. I can find API's that use the
> suggested name, but they vary in functionality (some overlap).
>
> There's also XmlNode.SelectSingleNode from C#, but sounds pretty horrible
> to me, variations on it might be:
>
> selectPath
> singlePath
> singleNode
>
> None of them really sound right to me, but may provoke someone else to do
> better.
>
> Over to you, someone else ...
>
> Cheers
> Joe
>
> On Sat, Jan 7, 2017 at 11:02 PM, Rowan Collins <[email protected]>
> wrote:
>
>>> On 07/01/2017 19:29, Joe Watkins wrote:
>>>
>>> Evening Roawn,
>>>
>>> There is no competition to see which single problem we are going to
>>> solve, we can fix more than one thing, obviously :)
>>>
>>> What we are discussing here is the value of this particular feature, not
>>> the value of things that nobody has taken the time to propose, implement,
>>> or fix.
>>>
>>> This contributor has waited a year for this conversation to start, it
>>> would be nice if we could stay focused.
>> Indeed, I didn't mean to imply otherwise, and sorry if I gave that
>> impression. There is a small cost to adding new methods like this, in
>> future maintenance, and the future consistency of the extension, but you're
>> right that it's not a big deal, and other than finding the right name, I've
>> no real reason to object to this method.
>>
>>
>> Regards,
>>
>> --
>> Rowan Collins
>> [IMSoP]
>>
>>
>> --
>> PHP Internals - PHP Runtime Development Mailing List
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Willian Gustavo Veiga
[PHP-DEV] Re: SimpleXML's atXPath method
April 18, 2017 03:40AM
We don't have enough opinions to continue.

Could anybody else contribute to this discussion? This way, we can
decide the future of the PR #1717.

Thank you very much, Willian Gustavo Veiga.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login