SimpleXML emptiness
| From: | Sara Golemon | Date: | Mon, 02 Jul 2007 20:41:36 +0000 |
| Subject: | SimpleXML emptiness | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-30414@lists.php.net to get a copy of this message | ||
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