talideon.com

There’s a fine line between genius and insanity... that’s where I live, baby!

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.

Technorati Search Technorati Search Irish Bloggers

Comments

No comments.

Post a comment

All form information is optional, but it’s a good idea to fill in your name and email address if you want me to take your comment seriously.

Spammers, don’t bother posting crap down here. The site is set up so that legitimate search engines (Google, for instance) won’t index pages with comments on them. Posting crud here only means you’re wasting my time and patience. Shoo!

Real names, please. Please include!
Won’t be displayed. Please include!
Displayed, if present.