|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-02-06 23:28 UTC] 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. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 08:00:01 2025 UTC |
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.