Bug #60517 [Opn->Nab]: SimpleXML doesn't like empty elements with attributes

From: Date: Fri, 01 May 2015 18:41:00 +0000
Subject: Bug #60517 [Opn->Nab]: SimpleXML doesn't like empty elements with attributes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192442@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 Updated by: cmb@php.net Reported by: raj at rajsingh dot org Summary: SimpleXML doesn't like empty elements with attributes -Status: Open +Status: Not a bug Type: Bug Package: SimpleXML related Operating System: Mac OS X 10.7.2 PHP Version: 5.4.0RC3 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2012-01-03 19:44:06] 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. ------------------------------------------------------------------------ [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

« previous php.bugs (#192442) next »