php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42034 Attributes not deleted properly
Submitted: 2007-07-18 16:15 UTC Modified: 2007-07-19 11:41 UTC
From: mail_ben_schmidt at yahoo dot com dot au Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5CVS-2007-07-18 (snap) OS: Mac OS X
Private report: No CVE-ID: None
 [2007-07-18 16:15 UTC] mail_ben_schmidt at yahoo dot com dot au
Description:
------------
In SimpleXML, when deleting attributes via 'unset', attributes having the same namespace as the element (or some such wrong namespace) are deleted rather than the non-prefixed one which is the one actually appearing in the 'array' (or not appearing--it can still be deleted if it does not appear).

Reproduce code:
---------------
$str=<<<DONE
<?xml version="1.0" encoding="utf-8"?>
<root
	xmlns="http://localhost/a"
	xmlns:ns="http://localhost/a"
	xmlns:other="http://localhost/b"
>
<elem attr="abc"/>
<elem ns:attr="abc"/>
<elem other:attr="abc"/>
<ns:elem attr="abc"/>
<ns:elem ns:attr="abc"/>
<ns:elem other:attr="abc"/>
<other:elem attr="abc"/>
<other:elem ns:attr="abc"/>
<other:elem other:attr="abc"/>
</root>
DONE;

$xml = simplexml_load_string($str);
foreach ($xml->elem as $elem) {
	unset($elem['attr']);
}
foreach ($xml->children('http://localhost/a')->elem as $elem) {
	unset($elem['attr']);
}
foreach ($xml->children('http://localhost/b')->elem as $elem) {
	unset($elem['attr']);
}
echo $xml->asXML();


Expected result:
----------------
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://localhost/a" xmlns:ns="http://localhost/a" xmlns:other="http://localhost/b">
<elem/>
<elem ns:attr="abc"/>
<elem other:attr="abc"/>
<ns:elem/>
<ns:elem ns:attr="abc"/>
<ns:elem other:attr="abc"/>
<other:elem/>
<other:elem ns:attr="abc"/>
<other:elem other:attr="abc"/>
</root>


Actual result:
--------------
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://localhost/a" xmlns:ns="http://localhost/a" xmlns:other="http://localhost/b">
<elem/>
<elem/>
<elem other:attr="abc"/>
<ns:elem attr="abc"/>
<ns:elem/>
<ns:elem other:attr="abc"/>
<other:elem attr="abc"/>
<other:elem ns:attr="abc"/>
<other:elem/>
</root>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-19 11:41 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The attributes are accessed based on the namespace scope the SimpleXMLElement object is currently in.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 12:01:30 2024 UTC