php.net |  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
Description:
------------
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: 1.151.2.22.2.33 $

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

Reproduce code:
---------------
<?php

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

$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="">
        <myns:child><grandchild>hello</grandchild></myns:child>
</root>

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-11 15:57 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

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-2022 The PHP Group
All rights reserved.
Last updated: Thu Dec 01 20:05:53 2022 UTC