|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46769 addAttributes requires prefix even when namespace is previously declared
Submitted: 2008-12-05 16:51 UTC Modified: 2008-12-16 14:30 UTC
Avg. Score:3.8 ± 1.3
Reproduced:6 of 7 (85.7%)
Same Version:2 (33.3%)
Same OS:1 (16.7%)
From: keith at blacknight dot com Assigned:
Status: Wont fix Package: SimpleXML related
PHP Version: 5.2.7 OS: Ubuntu 8.10
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-12-05 16:51 UTC] keith at blacknight dot com
Before bug #43221 <> was fixed, addAttribute did not requires attribute names to be qnames where the namespace has previously been declared on a parent element of the element to which the attribute was being added. Revision 1.246 <> broke this by requiring both that attributes with a namespace other than the default namespace *must* be prefixed regardless of whether the namespace has been declared with a prefix or not.

SimpleXML->addAttribute() should only require qnames be prefixed if and only if the namespace provided has not been previously declared.

Reproduce code:
$ns_foo = ",2008:foo";
$ns_xsi = "";

$root_doc = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xsi="$ns_xsi"
   xsi:schemaLocation=",2008 root.xsd"

$root = simplexml_load_string($root_doc);
$child = $root->addChild('bar', null, $ns_foo);
$child->addAttribute('xsi:schemaLocation', "$ns_foo foo.xsd", $ns_xsi);

Expected result:
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xsi="" xmlns=",2008" xmlns:foo=",2008:foo" xsi:schemaLocation=",2008 root.xsd"><foo:bar xsi:schemaLocation=",2008:foo foo.xsd"/></a>

Actual result:
Warning: SimpleXMLElement::addAttribute(): Attribute requires prefix for namespace in /home/keith/simplexml-regression.php on line 15
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xsi="" xmlns=",2008" xmlns:foo=",2008:foo" xsi:schemaLocation=",2008 root.xsd"><foo:bar/></a>


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-05 16:53 UTC] keith at blacknight dot com
Sorry, this line in the sample code:

$child->addAttribute('xsi:schemaLocation', "$ns_foo foo.xsd", $ns_xsi);

Should be:

$child->addAttribute('schemaLocation', "$ns_foo foo.xsd", $ns_xsi);
 [2008-12-12 17:40 UTC] keith at blacknight dot com
Problem also confirmed under FreeBSD 7.0 with PHP 5.2.8.
 [2008-12-16 14:30 UTC]
To maintain consistency, insure well-formed XML and prevent invalid 
internal XML structures, prefixes are required when creating namespaced 
attributes. SimpleXML will re-use an already bound prefix if in scope 
though. DOM should be used if a more liberal interface is desired.
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC