|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #42083 Attributes set in wrong namespace, and multiply defined
Submitted: 2007-07-24 03:17 UTC Modified: 2008-11-18 01:00 UTC
From: mail_ben_schmidt at yahoo dot com dot au Assigned:
Status: No Feedback Package: Documentation problem
PHP Version: 5.2CVS-2007-07-18 OS: Mac OS X
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-07-24 03:17 UTC] mail_ben_schmidt at yahoo dot com dot au
OK! This one really is a bug! I know it must be a bug as invalid XML is produced that can't be parsed! I have also tried it with php5.2-200707181030, though just running the binary in sapi/cli/php rather than doing make install.

When attributes are set or added using array notation, this is not done according to the currently active scope of the SimpleXMLElement in use.

Deletion and access work as expected, however. Thus a newly added/modified attribute cannot then be deleted or accessed without changing namespaces to the unnamed namespace by use of attributes().

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

$xml = simplexml_load_string($str,'SimpleXMLElement',0,'http://localhost/a');
foreach ($xml->children('http://localhost/a') as $elem) {
	echo $elem['attr']."\n";
echo $xml->asXML();

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

Actual result:
[begins with four blank lines]

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://localhost/a" xmlns:ns="http://localhost/a">
<elem attr="abc" attr="new" attr="three"/>
<elem attr="new" attr="three"/>
<ns:elem attr="abc" attr="new" attr="three"/>
<ns:elem attr="new" attr="three"/>


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-24 04:03 UTC] mail_ben_schmidt at yahoo dot com dot au
Also forgot to explicitly mention that the same attribute can also be added twice using array notation, thereby generating invalid XML.
 [2007-08-17 20:04 UTC]
Where is it documented that you're allowed to modify the xml document like this? For me this just looks like abuse.
 [2007-08-18 01:36 UTC] mail_ben_schmidt at yahoo dot com dot au
From the SimpleXML functions page in the manual (just prior to one of the later examples):

"Data in SimpleXML doesn't have to be constant. The object allows for manipulation of all of its elements."

I think setting attributes comfortably fits into 'all of its elements', although 'elements' is probably a poor word to choose in this context.

My understanding is that SimpleXML is just meant to be consistent and logical, so that it doesn't need lots of specific documentation. You can manipulate its objects like you would expect to manipulate any other PHP objects: unsetting deletes stuff, assigning changes stuff, or adds stuff, enumerating goes through stuff. And it works for subelements, attributes, whatever. If it's not allowed you get a warning or error, not a silent failure or invalid XML document. That said, the extension is not all that well documented yet, something I have volunteered to help with a bit, and will do when I have some time (I must say, I was a bit scared off by the length of the documentation about the documentation...but I hope to get to it before too long).

 [2007-08-19 20:06 UTC]
 [2007-08-22 16:10 UTC]
So we should change the documentation that modification of a SimpleXML object results in undefined behavior and people should use XMLWriter for such purposes instead I think. Not sure about this, so if anyone could confirm?
 [2008-11-10 11:18 UTC]
SimpleXML should be marked as read-only? I thought it can also create documents and this really seems like a bug.
 [2008-11-18 01:00 UTC] doc-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Mar 04 00:01:23 2021 UTC