php.net |  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:
 [2007-07-10 08:17 UTC] hubert dot roksor at gmail dot com
Description:
------------
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: http://www.w3.org/TR/REC-xml-names/#defaulting) 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: 1.151.2.22.2.32 $
libxslt Version => 1.1.17

Reproduce code:
---------------
<?php
$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="http://www.w3.org/1999/XSL/Transform" xmlns:myns="http://myns">

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

</xsl:stylesheet>'));

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"?>

                [hello]

namespaces: Array
(
)

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

                [hello]

namespaces: Array
(
)

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

                []

namespaces: Array
(
    [] =>
)

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

                [hello]

namespaces: Array
(
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-10 12:27 UTC] rrichards@php.net
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
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 13:02:46 2014 UTC