php.net |  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
Votes:4
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
 [2011-12-14 03:01 UTC] raj at rajsingh dot org
Description:
------------
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 = "http://www.rajsingh.org/poiwg/c_error.xml";
$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: http://www.rajsingh.org/pois/45343489.xml
link href: http://en.wikipedia.org/wiki/Boston
link href: http://www.geonames.org/maps/google_42.358_-71.06.html
number of relationships: 3
relationship term: contains
relationship term: within
relationship term: contains


Actual result:
--------------
number of links: 3
link href: http://www.rajsingh.org/pois/45343489.xml
link href: http://en.wikipedia.org/wiki/Boston
link href: http://www.geonames.org/maps/google_42.358_-71.06.html


Patches

Pull Requests

History

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 http://www.php.net/manual/en/simplexml.examples-basic.php 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] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-05-01 18:40 UTC] cmb@php.net
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 http://www.php.net/support.php 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-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 12:01:30 2024 UTC