php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54354 Can't not inherit parent's namespace (patch)
Submitted: 2011-03-23 05:58 UTC Modified: 2014-07-03 06:58 UTC
Votes:3
Avg. Score:4.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: tom at samplonius dot org Assigned: rrichards (profile)
Status: Not a bug Package: SimpleXML related
PHP Version: 5.3.6 OS: CentOS 4
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tom at samplonius dot org
New email:
PHP Version: OS:

 

 [2011-03-23 05:58 UTC] tom at samplonius dot org
Description:
------------
There are two issues:
(1) If you addChild(), the new child always inherits the namespace of the parent.  It is currently impossible to add children with no namespace, to a parent with a namespace.  Though looking at the code, it is clear that is intent to distinguish between a NULL namespace value and a blank namespace value.  I believe the design intent was that NULL means inherit the parent's namespace, and '' means that no namespace should be used.  The attached patch does this.

(2) If you specify a namespace of "", addChild() will actually add an attribute of " xmlns="" ", which is not valid.

Patch is attached.  If the $namespace parameter is "", no namespace is used, which fixes both issues.

Test script:
---------------
<?php
header('Content-Type: text/plain');

$r = new SimpleXMLElement('<r />');
$c1 = $r->addChild('ns1:child1', NULL, 'urn:myspace1');
$c1->addChild('child2');
echo $r->asXML(), "\n";

$r = new SimpleXMLElement('<r />');
$r->addChild('Thing1', 100, '');
echo $r->asXML(), "\n";


Expected result:
----------------
<?xml version="1.0"?>
<r><ns1:child1 xmlns:ns1="urn:myspace1"><ns1:child2/></ns1:child1></r>

<?xml version="1.0"?>
<r><Thing1>100</Thing1></r>

Actual result:
--------------
<?xml version="1.0"?>
<r><ns1:child1 xmlns:ns1="urn:myspace1"><ns1:child2/></ns1:child1></r>

<?xml version="1.0"?>
<r><Thing1 xmlns="">100</Thing1></r>

Patches

simplexml.c.patch (last revision 2011-04-26 23:07 UTC by tom at samplonius dot org)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-24 22:54 UTC] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: rrichards
 [2014-07-01 14:55 UTC] giacomo at boticca dot com
This is a tiny patch that makes a lot of sense, but it's been sitting there for more than 3 years.
Should it be a pull request instead of a patch now?
 [2014-07-03 06:58 UTC] rrichards@php.net
-Status: Assigned +Status: Not a bug
 [2014-07-03 06:58 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

Didn't notice this was open. Works as expected
xmlns="" is valid and means no default namespace. It is explicitly added to insure that no namespace be attached to the new child. Your patch actually breaks behavior and creates invalid (or at least not correct) XML. i.e.

$r = simplexml_load_string('<r xmlns="urn:myspace1" />');
$r->addChild('Thing1', 100, '');
echo $r->asXML(), "\n";

without patch (correct output):
<?xml version="1.0"?>
<r xmlns="urn:myspace1"><Thing1 xmlns="">100</Thing1></r>

with patch (incorrect as child maintains default namespace although explicitly told not to)
<?xml version="1.0"?>
<r xmlns="urn:myspace1"><Thing1>100</Thing1></r>
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Dec 09 02:05:54 2022 UTC