php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44066 Inconsistent behaviour when using unset on SimpleXMLElements
Submitted: 2008-02-06 23:28 UTC Modified: 2008-03-21 11:00 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: freddie at witherden dot org Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.5 OS: OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: freddie at witherden dot org
New email:
PHP Version: OS:

 

 [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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-07 00:33 UTC] 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-09 19:26 UTC] 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-10 07:01 UTC] 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-10 10:48 UTC] freddie at witherden dot org
> 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.
 [2008-03-21 11:00 UTC] rrichards@php.net
For functionality like that you would need to import the nodes from the 
xpath output into DOM and use its capabilities to get the parent and 
then remove the child.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jun 14 08:01:34 2024 UTC