|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41955 addChild() with no namespace defined ignores the default namespace
Submitted: 2007-07-10 20:53 UTC Modified: 2007-07-11 15:57 UTC
From: hubert dot roksor at gmail dot com Assigned: rrichards (profile)
Status: Not a bug Package: SimpleXML related
PHP Version: 5CVS-2007-07-10 (snap) OS:
Private report: No CVE-ID: None
 [2007-07-10 20:53 UTC] hubert dot roksor at gmail dot com
When adding a non-namespaced element to namespaced one, the new element is created under its parent's namespace instead of the scope's default namespace.

In the reproduce code below we create a tree whose root element carries an empty default namespace declaration, with one "child" element under a custom namespace. We use addChild() to add a grandchild with no namespace defined. As per my understanding of XML Namespaces specifications, that element should be created under the default namespace and since the default namespace is empty the element should not use any namespace at all.

A few notes:
- moving the default namespace declaration to the child node does not change the outcome
- using a non-empty default namespace does not change the outcome
- specifying an empty namespace using addChild()'s third parameter gives the expected result

Tested on:
PHP 5.2.4-dev (cli) (built: Jul 10 2007 12:04:20)
libXML Version => 2.6.26
SimpleXML Revision => $Revision: $

Thanks for reading, and thanks for fixing my previous bugs so quickly ;)

Reproduce code:

$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?>
<root xmlns:myns="http://myns" xmlns="">
	<myns:child />

$children = $xml->children('http://myns');
$children[0]->addChild('grandchild', 'hello');

echo $xml->asXML();


Expected result:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:myns="http://myns" xmlns="">

Actual result:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:myns="http://myns" xmlns="">


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-11 15:57 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Behavior is specific to simplexml. You are in the http://myns namespace scope (from $xml->children('http://myns');) and the child is created within that scope unless otherwise specified.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Mar 04 03:01:30 2024 UTC