#44066 [Fbk->Opn]: Inconsistent behaviour when using unset on SimpleXMLElements

From: Date: Sun, 10 Feb 2008 10:48:10 +0000
Subject: #44066 [Fbk->Opn]: Inconsistent behaviour when using unset on SimpleXMLElements
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-122147@lists.php.net to get a copy of this message
ID: 44066 User updated by: freddie at witherden dot org Reported By: freddie at witherden dot org -Status: Feedback +Status: Open Bug Type: SimpleXML related Operating System: OS X PHP Version: 5.2.5 New Comment: > can you provide an example where removing using offsets doesn't work (i.e. unset($xml->test[0]);)? I have just tried to replicate this, but alas, cannot. Sorry about that. > Calling unset in your prev example just unsets the variable. That functionality makes it very hard to unset the results of an xpath query. This is because one just has an array of nodes, making unsetting them extremely difficult. Previous Comments: ------------------------------------------------------------------------ [2008-02-10 07:01:15] rrichards@php.net can you provide an example where removing using offsets doesn't work (i.e. unset($xml->test[0]);)? The other things you mentioned are expected behavior. Calling unset in your prev example just unsets the variable. You must call unset on a member of an SimpleXMLElement in order to remove it (i.e. unset($sXML->test[1]);) ------------------------------------------------------------------------ [2008-02-09 19:26:38] freddie at witherden dot org Here is the problem with XPath: <?php $xml = <<<XML <?xml version='1.0'?> <document> <test>foo</test> <test>bar</test> <test>baz</test> </document> XML; $sXML = simplexml_load_string($xml); $arr = $sXML->xpath('/document/test'); foreach ($sXML->xpath('/document/test') as $element) unset($element); echo $sXML->asXML(); ?> Expected is that there are no <test> nodes, actual is that the document is unchanged. Yet, in the foreach loop if I add $element['attr'] = 'Attribute'; (so replacing the unset line) the document is modified (all of the <test> nodes have <test attr="Attribute">...</test>). So I can modify xpath results yet I can not unset them, The same applies to foreach ($sXML->test as $element), one can change but not unset. ------------------------------------------------------------------------ [2008-02-07 00:33:44] rrichards@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2008-02-06 23:28:11] freddie at witherden dot org Description: ------------ Calling unset on a SimpleXMLElement is inconsistent. Assuming $xml is a SimpleXMLElement and test is an 'array'. unset($xml->test); works, yet unset($xml->test[0]); (or any number does not). Elements returned from xpath queries can also not be unset. unset should work on all SimpleXMLElements, irrespective of where in a document they reside/how they were acquired. Failing this behaviour it would seem logical to generate a warning in the instance where one tries to unset an element which can not be unset. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=44066&edit=1

« previous php.bugs (#122147) next »