XML technologies / RSS & Atom / XML Namespaces

(Excerpt from "Newsfeeds with RSS and Atom" by Heinz Wittenbrink, published by Galileo Press, 2005)

The namespace mechanism is wrongly considered complicated, since it rests on a simple principle: assigning the names used in an XML document unambiguously to an XML vocabulary. This vocabulary forms the "namespace" to which those names belong.

XML documents use names for elements and attributes. An element’s name appears between the angle brackets of its tags; attribute names appear inside tags before an equals sign. In the example

<?xml version="1.0"?>
<feed version="0.8">
   <head></head>
</feed> 

, feed and channel are the names of two elements. (It is incorrect to speak of a feed or head tag when referring to the entire element as a container.) One also says an element has the name feed or an element is of type feed. The string version inside the start tag of the element feed is an attribute name.

These element and attribute names are known as local names. The names alone do not reveal their vocabulary. An application receiving this document cannot determine, for example, whether it is an Atom document.

To express unambiguously how the names feed and head should be interpreted, they must be "qualified" by explicitly associating element identifiers with their namespace. This association uses the attribute xmlns. Its value is a URI. In the following example

<?xml version="1.0"?>
<feed version="0.8" xmlns="http://purl.org/atom/ns#draft-ietf-atompub-format-04">
   <head></head>
</feed> 

, the attribute xmlns declares a namespace. feed and head are consequently qualified names, because the declared namespace applies both to the element carrying the declaration and to every element inside it. (Unless, as we will see, a descendant is assigned its own namespace.)

Globally unique identifiers

To illustrate qualification, we can write the element identifiers in expanded form as follows (this is not valid XML, but a teaching notation following XML Namespaces by James Clark):

<?xml version="1.0"?>
<{http://purl.org/atom/ns#draft-ietf-atompub-format-04}feed {http://purl.org/atom/ns#draft-ietf-atompub-format-04}version="0.4">
   <{http://purl.org/atom/ns#draft-ietf-atompub-format-04}head></{http://purl.org/atom/ns#draft-ietf-atompub-format-04}head>
</{http://purl.org/atom/ns#draft-ietf-atompub-format-04}feed> 

These qualified names are no longer local but globally unique names.

The reason is simple: a URI such as "http://purl.org/atom/ns#draft-ietf-atompub-format-08" is globally unique and has the same meaning in every document. No document even needs to exist at the address used as the URI. Internet naming rules ensure that this identifier is not used with different meanings.

Avoiding name conflicts

We can now insert an element from another namespace, using the XML version of the Dublin Core. The Dublin Core Metadata Element Set was developed by librarians as a vocabulary for online bibliographic information. It can be combined with all syndication vocabularies presented here. We select the element named author from that vocabulary and first insert it without specifying a namespace:

<?xml version="1.0"?>
<feed version="0.8" xmlns="http://purl.org/atom/ns#draft-ietf-atompub-format-04">
   <head>
      <author>James Clark</author>
   </head>
</feed> 

Written this way, the element cannot be recognised as Dublin Core by an Atom processor. Atom also has an element named author; however, its content must consist of other elements, including a required element named name, as in the following example:

<?xml version="1.0"?>
<feed version="0.8" xmlns="http://purl.org/atom/ns#draft-ietf-atompub-format-08">
   <head>
      <author>
         <name>James Clark</name>
      </author>
   </head>
</feed> 

To specify that the element author comes from Dublin Core rather than Atom, we must assign it to that vocabulary’s namespace, as in the following code example:

<?xml version="1.0"?>
<feed version="0.8" xmlns="http://purl.org/atom/ns#draft-ietf-atompub-format-04">
   <head>
      <author xmlns="http://purl.org/dc/elements/1.1/">James Clark</author>
   </head>
</feed>

In our teaching notation with prefixed URIs, this document would look like this:

<?xml version="1.0"?>
<{http://purl.org/atom/ns#draft-ietf-atompub-format-04}feed {http://purl.org/atom/ns#draft-ietf-atompub-format-04}version="0.8">
   <{http://purl.org/atom/ns#draft-ietf-atompub-format-08}head>
      <{http://purl.org/dc/elements/1.1/}author>James Clark</{http://purl.org/dc/elements/1.1/}author>
   </{http://purl.org/atom/ns#draft-ietf-atompub-format-08}head>
</{http://purl.org/atom/ns#draft-ietf-atompub-format-08}feed> 


The element author is now qualified too, so it cannot be confused with the element author from Atom. Both elements have the same local name but different qualified names. They can even be combined as follows:

<?xml version="1.0"?>
<feed version="0.8" xmlns="http://purl.org/atom/ns#draft-ietf-atompub-format-04">
   <head>
      <author>
         <name>James Clark</name>
      </author>
      <author xmlns="http://purl.org/dc/elements/1.1/">James Clark</author>
   </head>
</feed> 

An application understanding only Atom elements can ignore the Dublin Core element author in this document. A Dublin Core application can instead recognise an author’s name supplied according to that standard.

That is (almost) all! We have covered namespace basics. Problems for many users and developers arise because namespace assignments can also use abbreviations, avoiding repetition of the attribute xmlns with a URI as its value.

Instead, a string can be defined to represent this attribute together with one of its values. The following example uses abbreviations for both namespaces:

<?xml version="1.0"?>
<atom:feed version="0.8" xmlns:atom="http://purl.org/atom/ns#draft-ietf-atompub-format-04" xmlns:dc="http://purl.org/dc/elements/1.1/">
   <atom:head>
      <atom:author>
         <atom:name>James Clark</atom:name>
      </atom:author>
      <dc:author>James Clark</dc:author>
   </atom:head>
</atom:feed> 

XML documents containing elements or attributes from different namespaces generally use this notation. Names consist of a prefix, a colon and the local name. A document’s root element can also declare a default namespace with the attribute xmlns. This namespace applies to every element without a prefix. The previous example can therefore also be written as follows:

<feed version="0.8" xmlns="http://purl.org/atom/ns#draft-ietf-atompub-format-08" xmlns:dc="http://purl.org/dc/elements/1.1/">
   <head>
      <author>
         <name>James Clark</name>
      </author>
      <dc:author>James Clark</dc:author>
   </head>
</feed> 

For an XML parser—an application extracting information from XML for further processing—these versions mean the same thing. They have the same Infoset. They yield identical information, differing only in "serialisation", the representation of information as a sequence of characters.

A namespace prefix means only that a local name is associated with a URI. The prefix itself is arbitrary and generally chosen by the document’s author. The following example represents the same Infoset as the previous one, despite using different prefix strings:

<atom:feed version="0.4" xmlns:syndication="http://purl.org/atom/ns#draft-ietf-atompub-format-04" xmlns:metadata="http://purl.org/dc/elements/1.1/">
   <syndication:head>
      <syndication:author>
         <syndication:name>James Clark</syndication:name>
      </syndication:author>
      <metadata:author>James Clark</metadata:author>
   </syndication:head>
</atom:feed>

To repeat: an XML parser uses these prefixes only to associate local names with a URI, which identifies the namespace. In practice, however, certain prefixes have become conventional, such as dc for Dublin Core or xsl for XSLT stylesheets.

Unfortunately, not every RSS or Atom document can be assumed to be processed by an application with a complete XML parser. Parsing often fails to represent and pass on the full Infoset. Many RSS applications recognise only a fixed vocabulary and treat qualified names as local names. Some RSS standards therefore prescribe namespace prefixes, for example for modules extending RSS 1.0. Authors and developers are well advised to use conventional namespace prefixes even when XML itself does not require them.

All formats presented in detail here use XML namespaces. RSS 2.0, however, was defined without its own namespace. Older syndication formats (RSS 0.9x) do not yet use this technique. XML namespaces are the main prerequisite for extending XML vocabularies.

   

 

 

 

Creative Commons license
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

back to top