PHP SimpleXML is broken: Part 1

Jan 02, 2016 18:24


The following code does not work as expected:

$xml = simplexml_load_string("text"); $node = $xml->xpath("/root/a")[0]; if ($node) process($node); // process($node) may not be called for some valid nodes

Unlike most other objects, SimpleXmlElement may evaluate as false even when it is not null. Specifically, SimpleXmlElements representing empty ( Read more... )

hacker's diary, software development, web development

Leave a comment

dennisgorelik January 3 2016, 00:43:10 UTC
> so whatever goes inside the “if” will be skipped for them.

What does "goes inside the “if”" mean?
In your example "$node" is inside of "if", right?

Do you mean that if XPath found empty element, then resulting $node would be null?

Or you mean that if input XML contains empty node, then XPath search result would always evaluate to null even if it found not empty node?

Reply

yatur January 3 2016, 00:48:37 UTC
Everything that may be misunderstood will be misunderstood.
I fixed the example, so it is now (hopefully) clearer.

Reply

dennisgorelik January 3 2016, 00:57:54 UTC
> Everything that may be misunderstood will be misunderstood.

Of course.

> I fixed the example

I do not see what you fixed.

If your xpath was "/root/a" instead of "/root/b" it would be more clear I guess.
But your current xpath example is searching for node that is not empty.

Reply

yatur January 3 2016, 01:05:52 UTC
> I do not see what you fixed.

The 'goes inside if' part.

> But your current xpath example is searching for node that is not empty.

Well, that's the bug I found. The node is not empty, but PHP thinks it is. But I agree, it's confusing in this setting. Changed query to /root/a.

Reply

dennisgorelik January 3 2016, 01:17:38 UTC
Ahh, so it is combination of two things:
1) Weird interpretation of "if(object)".
2) Xpath bug that sometimes treats non-empty nodes - as if they were empty.
Right?

Reply


Leave a comment

Up