# SimpleXML emptiness

- Thread: https://externals.io/message/30414
- Original archive: https://news-web.php.net/php.internals/30414
- Started by Sara Golemon on 2007-07-02T20:41:36+00:00
- Messages: 2
- 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

---

## #30414 — Sara Golemon — 2007-07-02T20:41:36+00:00

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

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

---

## #30426 — David Coallier — 2007-07-03T14:37:44+00:00

URL: https://externals.io/message/30414#30426 · In reply to: #30414

> 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;
> +                               }
>                         }
>                 }
>         }
>
>
> --
> 
> 
>

Tested it and makes perfect sense.

-- 
David Coallier,
Founder & Software Architect,
Agora Production (http://agoraproduction.com)
51.42.06.70.18
