XML Default namespaces for unqualified attribute names?
Asked Answered
S

4

30

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.

Sheree answered 22/7, 2010 at 19:0 Comment(0)
B
18

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.

Because answered 22/7, 2010 at 21:11 Comment(4)
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.Sheree
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.Because
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.Sheree
You pretty much explained that better than I did :)Because
A
11

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 OK, even though an element cannot have two attributes 
     with the same name -->
<x xmlns:n1="http://www.w3.org" 
   xmlns="http://www.w3.org" >
  <good a="1"     n1:a="2" />
</x>

This is conformant because the two attributes are indeed in two different namespaces:

  • n1:a belongs to http://www.w3.org namespace (which is the namespace of good as well)
  • a is treated to belong to an inaccessible namespace http://wwww.w3.org > good (and different from the namespace of good).

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 for namespace-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 (also http://www.w3.org)
  • a belongs to the Per Element Type Partition Of good (i.e., http://wwww.w3.org > good).

Here's the relevant part of the spec Porges linked to:

A.2 XML Namespace Partitions

In order to support the goal of making both qualified and unqualified names useful in meeting their intended purpose, we identify the names appearing in an XML namespace as belonging to one of several disjoint traditional (i.e. set-structured) namespaces, called namespace partitions. The partitions are:

The All Element Types Partition All element types in an XML namespace appear in this partition. Each has a unique local part; the combination of the namespace name and the local part uniquely identifies the element type.

The Global Attribute Partition This partition contains the names of all attributes which are defined, in this namespace, to be global. The only required characteristic of a global attribute is that its name be unique in the global attribute partition. This specification makes no assertions as to the proper usage of such attributes. The combination of the namespace name and the attribute name uniquely identifies the global attribute.

The Per-Element-Type Partitions Each type in the All Element Types Partition has an associated namespace in which appear the names of the unqualified attributes that are provided for that element. This is a traditional namespace because the appearance of duplicate attribute names on an element is forbidden by XML 1.0. The combination of the attribute name with the element's type and namespace name uniquely identifies each unqualified attribute.

In XML documents conforming to this specification, the names of all qualified (prefixed) attributes are assigned to the global attribute partition, and the names of all unqualified attributes are assigned to the appropriate per-element-type partition.

Asset answered 22/7, 2010 at 19:0 Comment(3)
Thanks, @Raghu-Dodda. This subtlety is a great example of why much of XML is overly complex leading to unnecessary bugs.Sheree
Thank you so much. It's good to know that the prefixed attributes and unprefixed attributes in your example both belong to the same namespace, but to different namespace-partitions.Annmarieannnora
Can we say that if attribute is defined by means of <xsd:attribute> (see this example), it gets into The Global Attribute Partition and hence needs to be prefixed?Grocery
B
2

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.

Bewilder answered 22/7, 2010 at 21:6 Comment(2)
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?"Sheree
It is interpreted by the program that reads the XML document. It is not in the scope of the rec. You interpret it as you wish. Thus the creators of an XML vocabulary decide what is the meaning of the attributes in each and every element.Pyx
N
1

I've found the explicit explanation in "XML in a Nutshell" by Elliotte Rusty Harold:

The attributes are a different story. Default namespaces only apply to elements, not to attributes.

Northernmost answered 2/4, 2019 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.