Currently, an empty check like the following returns false because SimpleXML only checks for node existence in has_property. I'd like to propose the attached patch to change this to look at the node's children and apply PHP's emptiness rules to the content.

$data = '<?xml version="1.0 ?>
<doc>
  <e1></e1>
  <e2>0</e2>
  <f1>foo</f1>
  <x eprop="" fprop="data" />
</doc>';

$s = simplexml_load_string($data);
var_dump(empty($s->e1)); // Currently false
var_dump(empty($s->e2)); // Currently false
var_dump(empty($s->f1)); // False
var_dump(empty($s->x['eprop'])); // Currently false
var_dump(empty($s->x['fprop'])); // False

Note that this patch only effects elements which only contains an empty text node or only a text node with a literal "0". If the element contains real children, then it's considered to be non-empty (even if those children are in turn empty themselves).

-Sara

Index: ext/simplexml/simplexml.c
===================================================================
RCS file: /repository/php-src/ext/simplexml/simplexml.c,v
retrieving revision 1.151.2.23
diff -u -p -r1.151.2.23 simplexml.c
--- ext/simplexml/simplexml.c   1 Jan 2007 09:40:27 -0000       1.151.2.23
+++ ext/simplexml/simplexml.c   2 Jul 2007 20:30:05 -0000
@@ -692,6 +692,11 @@ static int sxe_prop_dim_exists(zval *obj
                                        attr = attr->next;
                                }
                        }
+                       if (exists && check_empty == 1 &&
+                               (!attr->children || !attr->children->content || 
!attr->children->content[0] || !xmlStrcmp(attr->children->content, "0")) ) {
+                               /* Attribute with no content in it's text node 
*/
+                               exists = 0;
+                       }
                }
 
                if (elements) {
@@ -714,6 +719,11 @@ static int sxe_prop_dim_exists(zval *obj
                        }
                        if (node) {
                                exists = 1;
+                                if (check_empty == 1 && 
+                                       (!node->children || 
(node->children->type == XML_TEXT_NODE && !node->children->next &&
+                                               (!node->children->content || 
!node->children->content[0] || !xmlStrcmp(node->children->content, "0")))) ) {
+                                       exists = 0;
+                               }
                        }
                }
        }

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to