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.
Your interpretation of the spec is correct. Some kind of rationale is also given in the second paragraph of section 6.2 in the namespaces spec you referenced:
But I would also be interested in some more details on why this specific behavior was chosen.
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.
Per the spec, you are correct to consider the namespace of the
attrib
in the first example to be empty. However, there is a subtlety here that may not be readily obvious.Consider this example further down in the spec of an element with two attributes with the same name (one prefixed and another unprefixed).
This is conformant because the two attributes are indeed in two different namespaces:
n1:a
belongs tohttp://www.w3.org
namespace (which is the namespace ofgood
as well)a
is treated to belong to an inaccessible namespacehttp://wwww.w3.org > good
(and different from the namespace ofgood
).Note that
http://wwww.w3.org > good
namespace does not exist; for example, you cannot query for attributes in this namespace with XPath. If you ask fornamespace-uri(\\good\a)
, it will be empty. To make the idea of a separate element namespace concrete, I made up a namespace that has both the element namespace and name together with a separator (>
is not allowed unescaped in attribute values anyways).Now, instead of saying that the two attributes are in two different namespaces, it is more correct to say that they belong to two different namespace partitions:
n1:a
attribute belongs to the Global Attribute Partition (http://www.w3.org
)good
element belongs to All Element Types Partition (alsohttp://www.w3.org
)a
belongs to the Per Element Type Partition Ofgood
(i.e.,http://wwww.w3.org > good
).Here's the relevant part of the spec Porges linked to: