Edit report at https://bugs.php.net/bug.php?id=60517&edit=1
ID: 60517
User updated by: raj at rajsingh dot org
Reported by: raj at rajsingh dot org
Summary: SimpleXML doesn't like empty elements with
attributes
Status: Open
Type: Bug
Package: SimpleXML related
Operating System: Mac OS X 10.7.2
PHP Version: 5.4.0RC3
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-12-24 09:37:41] 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.
------------------------------------------------------------------------
[2011-12-14 03:01:33] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60517&edit=1