October 2, 2007 at 2:01PM SimpleXML treats attribute namespaces incorrectly.
Joy! I just submitted this bug to PHP’s bugtracker:
Description:
------------
According to "Namespaces in XML 1.0 (Second Edition)", SS6.2:
"A default namespace declaration applies to all unprefixed element names
within its scope. Default namespace declarations do not apply directly
to attribute names; the interpretation of unprefixed attributes is
determined by the element on which they appear."
However, SimpleXML appears to treat unprefixed attributes as in the
default namespace. This is incorrect.
Reproduce code:
---------------
<?php
$doc1 = <<<LEFIN
<foo:bar xmlns:foo="urn:1">
<foo:baz fred="barney"/>
</foo:bar>
LEFIN;
$doc2 = <<<LEFIN
<foo:bar xmlns:foo="urn:1">
<foo:baz foo:fred="barney"/>
</foo:bar>
LEFIN;
$kids1 = simplexml_load_string($doc1)->children('urn:1');
$kids2 = simplexml_load_string($doc2)->children('urn:1');
print_r($kids1);
print_r($kids2);
Expected result:
----------------
SimpleXMLElement Object
(
[baz] => SimpleXMLElement Object
(
[@attributes] => Array
(
[fred] => barney
)
)
)
SimpleXMLElement Object
(
[baz] => SimpleXMLElement Object
(
[@attributes] => Array
(
[fred] => barney
)
)
)
Actual result:
--------------
SimpleXMLElement Object
(
[baz] => SimpleXMLElement Object
(
)
)
SimpleXMLElement Object
(
[baz] => SimpleXMLElement Object
(
[@attributes] => Array
(
[fred] => barney
)
)
)
This is a pretty dumb bug, especially within the context of this one. Interestingly, I found the same bug in a certain large domain registry’s external API...
The specification I’m referencing, and the specific section can be found here.
No comments.