# SimpleXML's atXPath method

- Thread: https://externals.io/message/97523
- Original archive: https://news-web.php.net/php.internals/97523
- Started by Willian Veiga on 2017-01-06T14:03:52+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #97523 — Willian Veiga — 2017-01-06T14:03:52+00:00

URL: https://externals.io/message/97523#97523

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.

---

## #97555 — Rowan Collins — 2017-01-07T16:26:36+00:00

URL: https://externals.io/message/97523#97555 · In reply to: #97523

> 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]

---

## #97557 — Joe Watkins — 2017-01-07T17:01:03+00:00

URL: https://externals.io/message/97523#97557 · In reply to: #97555

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 <rowan.collins@gmail.com>
wrote:

> 
>
> > […]
> 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]

---

## #97560 — Kalle Sommer Nielsen — 2017-01-07T17:37:05+00:00

URL: https://externals.io/message/97523#97560 · In reply to: #97557

2017-01-07 18:01 GMT+01:00 Joe Watkins <pthreads@pthreads.org>:
> 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

---

## #97561 — Rowan Collins — 2017-01-07T18:24:57+00:00

URL: https://externals.io/message/97523#97561 · In reply to: #97557

> 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]

---

## #97564 — Joe Watkins — 2017-01-07T19:29:42+00:00

URL: https://externals.io/message/97523#97564 · In reply to: #97561

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 <rowan.collins@gmail.com>
wrote:

> 
>
> > […]
>
> 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]

---

## #97569 — Rowan Collins — 2017-01-07T23:02:09+00:00

URL: https://externals.io/message/97523#97569 · In reply to: #97564

> 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]

---

## #97570 — Joe Watkins — 2017-01-07T23:19:20+00:00

URL: https://externals.io/message/97523#97570 · In reply to: #97569

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 <rowan.collins@gmail.com>
wrote:

> 
>
> > […]
> 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]

---

## #97572 — Stephen Reay — 2017-01-08T01:42:24+00:00

URL: https://externals.io/message/97523#97572 · In reply to: #97570

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

> 
> 
> 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 <rowan.collins@gmail.com>
> wrote:
> 
> > […]

---

## #98814 — Willian Gustavo Veiga — 2017-04-18T01:31:10+00:00

URL: https://externals.io/message/97523#98814 · In reply to: #97523

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.

---

## #98816 — Willian Gustavo Veiga — 2017-04-18T01:31:50+00:00

URL: https://externals.io/message/97523#98816 · In reply to: #97523

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.
