|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41947 SimpleXML incorrectly registers empty strings as namespaces
Submitted: 2007-07-10 08:17 UTC Modified: 2007-07-10 12:27 UTC
From: hubert dot roksor at gmail dot com Assigned:
Status: Closed Package: SimpleXML related
PHP Version: 5CVS-2007-07-10 (snap) OS:
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.
Block user comment
Status: Assign to:
Bug Type:
From: hubert dot roksor at gmail dot com
New email:
PHP Version: OS:


 [2007-07-10 08:17 UTC] hubert dot roksor at gmail dot com
As per XML Namespaces specifications, "The attribute value in a default namespace declaration MAY be empty. This has the same effect, within the scope of the declaration, of there being no default namespace." (source: However, when creating elements using an empty string as namespace, SimpleXML seems to register the empty string as a namespace instead of just removing any inherited namespace.

In the reproduce code below, we generate an empty tree to which we add a new element "child" in the namespace "http://myns". Then we add an element "grandchild" (whose content will be "hello") to that element using an empty string as namespace so that "grandchild" does not inherit "http://myns" as namespace. Then we attempt to transform the tree using XSLT, and verify grandchild's namespaces with getNamespaces().

Apparently, it is impossible to access "grandchild" in XSL because it belongs to an empty namespace using an empty prefix (as shown with getNamespaces()). Reloading the tree by dumping it as XML then reparsing it with simplexml_load_string() circumvents that bug.

Tested on:
PHP 5.2.4-dev (cli) (built: Jul 10 2007 00:04:16)
libXML Version => 2.6.26
SimpleXML Revision => $Revision: $
libxslt Version => 1.1.17

Reproduce code:
$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><root xmlns:myns="http://myns" />');
$grandchild = $xml->addChild('child', null, 'http://myns')->addChild('grandchild', 'hello', '');

$xslt = new XSLTProcessor;
$xslt->importStylesheet(simplexml_load_string('<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:myns="http://myns">

	<xsl:template match="myns:child">
		[<xsl:value-of select="grandchild" />]


echo 'output before reload: ', $xslt->transformToXML($xml), "namespaces: ", print_r($grandchild->getNamespaces(), true);

$xml = simplexml_load_string($xml->asXML());
$children = $xml->children('http://myns');
$grandchild = $children[0]->grandchild;

echo "\noutput after reload: ", $xslt->transformToXML($xml), "namespaces: ", print_r($grandchild->getNamespaces(), true);

Expected result:
output before reload: <?xml version="1.0"?>


namespaces: Array

output after reload: <?xml version="1.0"?>


namespaces: Array

Actual result:
output before reload: <?xml version="1.0"?>


namespaces: Array
    [] =>

output after reload: <?xml version="1.0"?>


namespaces: Array


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-10 12:27 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jun 20 18:01:30 2024 UTC