XML Default namespaces for unqualified attribute names

namespacesxmlxml-namespaces

I'm trying to understand the correct interpretation of the "Namespaces in XML 1.0 (Third Edition)" definition for unqualified attribute namespaces.

"The namespace name for an unprefixed attribute name always has no value."

And later in the same section:

"The attribute value in a default namespace declaration MAY be empty. This has the same effect, within the scope of the declaration, of there being no default namespace."

So if I want to declare a default namespace for an element (and its children), do I also have to declare a prefix-namespace mapping for any attributes which reside within that namespace?

For example, in this example

<parent xmlns="http://example.com/foo">
    <child attrib="value">text</child>
<parent>

I would interpret the above definition to say that the namespace of attrib is empty.

So if I needed attrib to have the same namespace as parent, then I would be forced to do this?

<foo:parent xmlns:foo="http://example.com/foo">
    <foo:child foo:attrib="value">text</foo:child>
<foo:parent>

or this?

<parent xmlns="http://example.com/foo" xmlns:foo="http://example.com/foo">
    <child foo:attrib="value">text</child>
<parent>

This seems silly to me as it appears to defeat the purpose of default namespaces. I'm hoping that I'm just misunderstanding the spec.

Best Answer

You're correct. The idea behind attributes not being part of the default namespace is that they are considered to exist in an "element namespace" — so in this case, <foo:child/> is considered to be the 'namespace' for @attrib. Note that this is just conceptual; there's no API or anything that refers to attribute namespaces this way.

This was chosen because multiple elements may have attributes with the same names, but different meanings — unlike a traditional namespace, which is a set of names (so no duplicates). In a way, it gives more structure to the namespace, instead of having a flat set.

You can read about this in a very old version of the Namespaces recommendation.

This convention means that whenever you see a prefixed attribute, it represents some 'additional' information which isn't related to the main schema in the document.

Related Topic