Jesper Tverskov, Marts 15, 2011

Attributes and XML namespaces

It is confusing for beginners in XML that the attributes of an element are not in a namespace if they don't have a proper namespace prefix. There is no default namespace for attributes. It is almost a rule that attributes are not in a namespace.

1. Attribute names must be unique

XML namespaces were invented to make it possible for markup coming from different XML applications to co-exist in the same document without element name collisions. The namespaces make identical element names from different XML applications different when expanded.

It is only logical that namespaces are mostly about elements and that attributes don’t need to be in a namespace. An element name can appear many times as child of the same parent element. But all attributes of an element must have unique names for the document to be well-formed. We can still have attributes with the same name in different parent elements. The parent elements will tell identical attribute names apart.

We often want to mix elements from different XML applications in the same document. But we almost never mix attributes from different XML applications in the same element. Name collisions are only a real danger for element names. [1]

2. Attributes are not children

An attribute is not considered a child of its parent element. An attribute never inherits the namespace of its parent element. For that reason an attribute is only in a namespace if it has a proper namespace prefix. An attribute can never be in a default namespace.

In the following XML document, the "id" attribute is in no namespace:

  1. <products xmlns="http://www.xmlplease.com">
  2.    <product id="p1"/>
  3. </products>

In the following XML document, the "id" attribute is in no namespace:

  1. <xp:products xmlns:xp="http://www.xmlplease.com">
  2.    <xp:product id="p1"/>
  3. </xp:products>

In the following XML document, the "id" attribute is in the "http://www.xmlplease.com" namespace.

  1. <xp:products xmlns:xp="http://www.xmlplease.com">
  2.    <xp:product xp:id="p1"/>
  3. </xp:products>

In the following XML document, the elements are in the "http://www.xmlplease.com/ns1" namespace and the "id" attribute is in the "http://www.xmlplease.com/ns2" namespace:

  1. <products xmlns=" http://www.xmlplease.com/ns1" xmlns:xp="http://www.xmlplease.com/ns2">
  2.    <product xp:id="p1"/>
  3. </products>

3. XML namespace and XML application

Markup based on XML is said to be an XML application. To tell XML applications apart when co-existing in the same document we use XML namespace declarations. It is not the namespace that makes markup an XML application. Markup belongs to a set of elements and attributes, an XML application, an XML vocabulary, if we say so. Namespaces are only a way to make it possible for elements of XML applications to co-exist in the same document.

A DTD or XML schema is also not necessary to have an XML application. DTD or schema are only an easy way to verify if markup belong to a particular XML application.


Attributes not in a namespace are still part of the same XML application as their parent elements in a namespace. We simply most often don't care about the attributes when declaring a namespace because we don't run into the problem of conflicting attribute names. They would make the XML document not well-formed and the XML processor will alert us right away.

4. When attributes need a namespace

We could have some extremely rare situations where we for some reason need to put attributes with the same name in an element, e.g.: two "id" attributes. We can do that by putting one or both attributes in a namespace with a prefix. In that way the qualified names become unique.

5. Attribute with prefix

Even though namespaces for attributes are extremely rare, they do exist. E.g.: WordprocessingML uses prefixes for its attributes. A tiny fragment of a WordprocessingML document could look like this:

  1. <wx:borders>
  2.    <wx:top wx:val="solid" wx:bdrwidth="10" wx:space="1" wx:color="auto"/>
  3.    <wx:left wx:val="solid" wx:bdrwidth="10" wx:space="4" wx:color="auto"/>
  4.    <wx:bottom wx:val="solid" wx:bdrwidth="10" wx:space="1" wx:color="auto"/>
  5.    <wx:right wx:val="solid" wx:bdrwidth="10" wx:space="4" wx:color="auto"/>
  6. </wx:borders>

The only benefit is that it is easy to add new attributes from another XML application and tell them apart when you see them or to make co-existence possible in the same element if they have the same local name. In e.g. "w:p" elements we see attributes using "wsp" prefix:

  1. <w:p wsp:rsidR="000945B6" wsp:rsidRDefault="000945B6" wsp:rsidP="000945B6"/>

I have a feeling that it was a wrong design decision to put all attributes in namespaces making WordProcessingML awful to look at for no very good reason?

6. Navigating XML in a namespace

An attribute without a prefix is never in a namespace. This has implications when we want to navigate XML with code. If the elements are in a namespace we must declare it also in the code. But in the code we must not declare namespaces for the attributes unless they actually are in a namespace.

An attribute is only in a namespace if it has a proper prefix declared as an alias for an XML namespace.

Footnotes

[1]

XML namespaces are defined in a standard (Recommendation) of its own: Namespaces in XML 1.0. It is not very explicit about attributes but it does say in 6.2 Namespace Defaulting: "Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear."

Updated: 2011-07-29