php.net |  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
Votes:8
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: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: keith at blacknight dot com
New email:
PHP Version: OS:

 

 [2008-12-05 16:51 UTC] keith at blacknight dot com
Description:
------------
Before bug #43221 <http://bugs.php.net/bug.php?id=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 <http://url.ie/yxb> 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:
---------------
<?php
$ns_foo = "tag:example.com,2008:foo";
$ns_xsi = "http://www.w3.org/2001/XMLSchema-instance";

$root_doc = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xsi="$ns_xsi"
   xmlns="tag:example.com,2008"
   xsi:schemaLocation="tag:example.com,2008 root.xsd"
   xmlns:foo="$ns_foo"/>
EOT;

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


Expected result:
----------------
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="tag:example.com,2008" xmlns:foo="tag:example.com,2008:foo" xsi:schemaLocation="tag:example.com,2008 root.xsd"><foo:bar xsi:schemaLocation="tag:example.com,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="http://www.w3.org/2001/XMLSchema-instance" xmlns="tag:example.com,2008" xmlns:foo="tag:example.com,2008:foo" xsi:schemaLocation="tag:example.com,2008 root.xsd"><foo:bar/></a>

Patches

Pull Requests

History

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] rrichards@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC