Bug #60517 [Com]: SimpleXML doesn't like empty elements with attributes

From: Date: Sat, 24 Dec 2011 09:37:41 +0000
Subject: Bug #60517 [Com]: SimpleXML doesn't like empty elements with attributes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-166073@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=60517&edit=1 ID: 60517 Comment by: will dot fitch at gmail dot com 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: 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. Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#166073) next »