|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60517 SimpleXML doesn't like empty elements with attributes
Submitted: 2011-12-14 03:01 UTC Modified: 2015-05-01 18:40 UTC
Avg. Score:4.0 ± 1.7
Reproduced:2 of 3 (66.7%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: raj at rajsingh dot org Assigned: cmb (profile)
Status: Not a bug Package: SimpleXML related
PHP Version: 5.4.0RC3 OS: Mac OS X 10.7.2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: raj at rajsingh dot org
New email:
PHP Version: OS:


 [2011-12-14 03:01 UTC] raj at rajsingh dot org
If the first XML element is an empty tag, e.g. <link term="no"/>, then no elements by that name will get recognized. However, if you have many, e.g. <link> elements, and you write at least the first one as a non-empty tag, e.g. <link term="no"></link> SimpleXML will recognize all the elements (even those with empty tags. A test program that uses a test file on my web site shows this problem.

Test script:
$fn = "";
$xml = simplexml_load_file($fn);

if ( !empty($xml->link) ) {
  echo "number of links: " . sizeof($xml->link) . "\n";
  foreach ($xml->link as $link) {
    echo "link href: " . $link['href'] . "\n";

if ( !empty($xml->relationship) ) {
  echo "number of relationships: " . sizeof($xml->location->relationship) . "\n";
  foreach ($xml->location->relationship as $rel) {
    echo "relationship term: " . $rel['term'] . "\n";

Expected result:
number of links: 3
link href:
link href:
link href:
number of relationships: 3
relationship term: contains
relationship term: within
relationship term: contains

Actual result:
number of links: 3
link href:
link href:
link href:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-24 09:37 UTC] will dot fitch at gmail dot com
I believe the issue is in your code.  When you check for:

if (!empty($xml->relationship))

You are checking the first element. In your XML, the first element of 
<relationship> is empty. There is no value in the element. However, in your 
<link> example, the first element is not empty.  It contains a new line 
character.  That is why !empty($xml->link) passes.

What you should be checking is this:

if (count($xml->relationship) > 0)

When you're verifying with empty(), you're checking the element - not the number 
of elements with that name within the node leaf.
 [2012-01-03 19:44 UTC] raj at rajsingh dot org
Thanks Will. That works.

However, I'm still a little confused. Example #4 from made me think that I'd get back an iterable object from the $xml->relationship access method, but example #2 on that same page makes it seem like you get back the value of the element if there's only a single element with that name? 

So thanks for your solution because it works, but if I'm understanding correctly those examples, then the interface is sending a mixed message.
 [2015-05-01 18:40 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-05-01 18:40 UTC]
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Thanks for explaining, Will.

Furthermore example #4 states:

| When multiple instances of an element exist as children of a
| single parent element, normal iteration techniques apply.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Nov 27 16:05:53 2022 UTC