views:

45

answers:

2

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"&gt;
    <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"&gt;
    <foo:child foo:attrib="value">text</foo:child>
<foo:parent>

or this?

<parent xmlns="http://example.com/foo" xmlns:foo="http://example.com/foo"&gt;
    <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.

+1  A: 

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:

the interpretation of unprefixed attributes is determined by the element on which they appear.

But I would also be interested in some more details on why this specific behavior was chosen.

Jörn Horstmann
That extra paragraph seems to imply that there is more information on the topic, but I can't seem to find it. My question to the W3C editor who wrote that is, "*How* is an unprefixed attribute interpreted given the element on which it appears?"
McKAMEY
+2  A: 
Porges
Thanks. I'm a little confused as to how to represent the tag being the context. It seems to me that if "`<foo:child />` is the 'namespace' for `@attrib`" then I would *not* need to add additional prefixes to provide the namespace. In other words, it would be unqualified which would be interpreted as being in whatever namespace the element is using. Is that correct? This would imply to me that my first example is in fact an okay way to write this.
McKAMEY
Yes, the convention is for attributes to be un-namespaced, like your first example. `"it would be unqualified which would be interpreted as being in whatever namespace the element is using"`... that's the idea, but note that the attribute will *not* report having the same namespace as the element, or the element as 'namespace'. It won't have any namespace — the attribute having the element as 'namespace' is just a convention.
Porges
OK I think I understand now.Generally the attributes are not given an explicit namespace because they are within the context of the element which may have a namespace. This would be written like example 1.If there was a specific namespace for an attribute this is usually outside the schema of the element and should be written like examples 2 or 3, but typically it wouldn't be the same namespace.
McKAMEY
You pretty much explained that better than I did :)
Porges