ID:               48601
 Comment by:       theultramage at gmail dot com
 Reported By:      theultramage at gmail dot com
 Status:           Open
 Bug Type:         SimpleXML related
 Operating System: Windows Vista
 PHP Version:      6CVS-2009-06-19 (snap)
 New Comment:

>From the mail of the main libxml developer
(http://mail.gnome.org/archives/xml/2009-June/msg00040.html):

> A NULL object or an empty node set should be treated in the same
way.
Also
>There is no iterator API.

So for one part, the current SimpleXML code has a bug because it does
not handle the NULL value correctly (should return empty array, not
FALSE). And second, there's currently no other solution.


Previous Comments:
------------------------------------------------------------------------

[2009-06-19 10:57:16] theultramage at gmail dot com

Description:
------------
SimpleXML's xpath() implementation contains code that returns FALSE for
some queries where it should be returning an empty container object.

In revision 1.79 of simplexml.c
(http://cvs.php.net/viewvc.cgi/php-src/ext/simplexml/simplexml.c?r1=1.78&r2=1.79)
the call to xmlXPathEval() was hotfixed to avoid crashing if the query
errored. However, the author also added a check against
"retval->nodesetval", treating NULL as an error situation. I believe
this was an incorrect decision.

There is a scenario where 'nodesetval' will be NULL - when querying for
the attribute of a non-matching element ('/foo/@attr' when there are no
'foo' nodes); in all other 'bad' queries that I tried, 'nodesetval' came
initialized and containing zero nodes. So, we can see that for certain
queries, simplexml will give an incorrect result. This was already
pointed out on 18-dec-2005 by an user comment on the php
simplexml::xpath documentation page.

I have sent an inquiry to the xml mailing list
(http://mail.gnome.org/archives/xml/2009-June/msg00019.html) asking for
clarification, however I believe that the current logic is incorrect. I
checked php's DOM module src and libxml's xpath1.c code example, and
they do not consider it a problem (DOM initializes empty DOMNodeList,
example sets count to 0).

Alternatively, the code should not be messing with what is apparently
internal structures, and instead use the API for processing, if one is
available.

Reproduce code:
---------------
<?php
        $xml = '<?xml version="1.0" encoding="utf-8"?><dummy/>';
        $db = new SimpleXMLElement($xml);
        if( FALSE !== $db->xpath("/foo/@attr") )
                echo "OK";
        else
                echo "Error!";
?>

Expected result:
----------------
xpath() should return an empty array.

Actual result:
--------------
xpath() returns FALSE.


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=48601&edit=1

Reply via email to