Home > Articles > Web Services > XML

An introduction to XML Namespaces

📄 Contents

  1. The Problem Namespaces Solves
  2. Namespaces
  3. About this Article
  • Print
  • + Share This
This article discusses the background behind XML Namespaces. It presumes a fundamental grasp of basic XML Syntax, and the concepts of an XML document and a DTD (Document Type Definition).
From the author of

The Problem Namespaces Solves

XML is extensible. So it says in the name: eXtensible Markup Language. The problem is that extensibility does not come free. In a distributed environment, extensibility must be managed to avoid conflicts. Namespaces is a solution to help manage XML extensibility.

Namespace can be defined as a mechanism to identify of XML elements. It places the name of the elements in a more global context: the namespace.

The namespace recommendation, published by the W3C, is available at http://www.w3.org/TR/REC-xml-names.

The namespace recommendation is relatively thin. The concepts are not difficult either. Unfortunately, this means that namespaces are often overlooked! Don't make that mistake; namespaces are essential for many XML applications.

Let's suppose you decide to publish your bookmarks. (There is heavy competition from Yahoo! but let's ignore this for a moment). Listing 1 shows what it might look like in XML. As a standalone document Listing 1 works perfectly.

Listing 1 A list of resources in XML.

<?xml version="1.0"?>
<references>
   <name>Macmillan</name>
   <link href="">http://www.mcp.com"/>
   <name>Pineapplesoft Link</name>
   <link href="">http://www.pineapplesoft.com/newsletter"/>
   <name>XML.com</name>
   <link href="">http://www.xml.com"/>
   <name>Comics.com</name>
   <link href="">http://www.comics.com"/>
   <name>Fatbrain.com</name>
   <link href="">http://www.fatbrain.com"/>
   <name>ABC News</name>
   <link href="">http://www.abcnews.com"/>
</references>

In practice, however, documents are seldom standalone. In a collaborative environment like the Web, people build on each others work. Somebody might take your list and rate it. The result would be Listing 2 (admittedly I'm biased).

Listing 2 The references with quality ratings.

<?xml version="1.0"?><references>
   <name>Macmillan</name>
   <link href="">http://www.mcp.com"/>
   <rating>5 stars</rating>
   <name>Pineapplesoft Link</name>
   <link href="">http://www.pineapplesoft.com/newsletter"/>
   <rating>5 stars</rating>
   <name>XML.com</name>
   <link href="">http://www.xml.com"/>
   <rating>4 stars</rating>
   <name>Comics.com</name>
   <link href="">http://www.comics.com"/>
   <rating>5 stars</rating>
   <name>Fatbrain.com</name>
   <link href="">http://www.fatbrain.com"/>
   <rating>4 stars</rating>
   <name>ABC News</name>
   <link href="">http://www.abcnews.com"/>
   <rating>3 stars</rating>
</references>

Listing 2 is the same document with one new element: rating. It is often desirable to extend an existing document to convey new information instead of designing new schemas from scratch.

Problems occur however if the extension is not managed. Suppose somebody else decides to rate the list with parental advisory. Listing 3 shows the result (ABC News might report on violence, hence its PG rating).

Listing 3 Another meaning for rating.

<?xml version="1.0"?><references>
   <name>Macmillan</name>
   <link href="">http://www.mcp.com"/>
   <rating>G</rating>
   <name>Pineapplesoft Link</name>
   <link href="">http://www.pineapplesoft.com/newsletter"/>
   <rating>G</rating>
   <name>XML.com</name>
   <link href="">http://www.xml.com"/>
   <rating>G</rating>
   <name>Comics.com</name>
   <link href="">http://www.comics.com"/>
   <rating>G</rating>
   <name>Fatbrain.com</name>
   <link href="">http://www.fatbrain.com"/>
   <rating>G</rating>
   <name>ABC News</name>
   <link href="">http://www.abcnews.com"/>
   <rating>PG</rating>
</references>

This is problematic. Listing 3 also is an extension to Listing 1 but the extension creates incompatibilities between Listing 2 and Listing 3. This is a very common problem: two people extend the same document in incompatible ways.

Things get really out of hand when trying to combine both ratings in a listing. The result would look like Listing 4 where the two ratings conflict with each other.

Listing 4 The combined listing.

<?xml version="1.0"?>
<references>
   <name>Macmillan</name>
   <link href="">http://www.mcp.com"/>
   <rating>5 stars</rating>
   <rating>G</rating>
   <name>Pineapplesoft Link</name>
   <link href="">http://www.pineapplesoft.com/newsletter"/>
   <rating>5 stars</rating>
   <rating>G</rating>
   <name>XML.com</name>
   <link href="">http://www.xml.com"/>
   <rating>4 stars</rating>
   <rating>G</rating>
   <name>Comics.com</name>
   <link href="">http://www.comics.com"/>
   <rating>5 stars</rating>
   <rating>G</rating>
   <name>Fatbrain.com</name>
   <link href="">http://www.fatbrain.com"/>
   <rating>4 stars</rating>
   <rating>G</rating>
   <name>ABC News</name>
   <link href="">http://www.abcnews.com"/>
   <rating>3 stars</rating>
   <rating>PG</rating>
</references>

The problem with Listing 4 is that software designed to operate with Listing 3 and filter offensive links would be completely lost. It wouldn't know what to do with the "4 stars" rating. The software should simply ignore quality rating tags but how to ignore quality rating tags if it cannot differentiate between the two rating tags?

The solution is obvious: use different names for the two ratings. Listing 5 renames the "quality" element as qa-rating and the "parental" element as pa-rating.

Listing 5 Using different names.

<?xml version="1.0"?><references>
   <name>Macmillan</name>
   <link href="">http://www.mcp.com"/>
   <qa-rating>5 stars</qa-rating>
   <pa-rating>G</pa-rating>
   <name>Pineapplesoft Link</name>
   <link href="">http://www.pineapplesoft.com/newsletter"/>
   <qa-rating>5 stars</qa-rating>
   <pa-rating>G</pa-rating>
   <name>XML.com</name>
   <link href="">http://www.xml.com"/>
   <qa-rating>4 stars</qa-rating>
   <pa-rating>G</pa-rating>
   <name>Comics.com</name>
   <link href="">http://www.comics.com"/>
   <qa-rating>5 stars</qa-rating>
   <pa-rating>G</pa-rating>
   <name>Fatbrain.com</name>
   <link href="">http://www.fatbrain.com"/>
   <qa-rating>4 stars</qa-rating>
   <pa-rating>G</pa-rating>
   <name>ABC News</name>
   <link href="">http://www.abcnews.com"/>
   <qa-rating>3 stars</qa-rating>
   <pa-rating>PG</pa-rating>
</references>
  • + Share This
  • 🔖 Save To Your Account