Menu

XML Namespaces by Example

January 19, 1999

Tim Bray

January 14th saw the arrival of a new W3C Recommendation, Namespaces in XML. "Recommendation" is the final step in the W3C process; the status means that the document is done, frozen, agreed-upon and official.

Namespaces are a simple and straightforward way to distinguish names used in XML documents, no matter where they come from. However, the concepts are a bit abstract, and this specification has been causing some mental indigestion among those who read it. The best way to understand namespaces, as with many other things on the Web, is by example.

So let's set up a scenario: suppose XML.com wanted to start publishing reviews of XML books. We'd want to mark the info up with XML, of course, but we'd also like to use HTML to help beautify the display. Here's a tiny sample of what we might do:


<h:html xmlns:xdc="http://www.xml.com/books"

        xmlns:h="http://www.w3.org/HTML/1998/html4">

 <h:head><h:title>Book Review</h:title></h:head>

 <h:body>

  <xdc:bookreview>

   <xdc:title>XML: A Primer</xdc:title>

   <h:table>

    <h:tr align="center">

     <h:td>Author</h:td><h:td>Price</h:td>

     <h:td>Pages</h:td><h:td>Date</h:td></h:tr>

    <h:tr align="left">

     <h:td><xdc:author>Simon St. Laurent</xdc:author></h:td>

     <h:td><xdc:price>31.98</xdc:price></h:td>

     <h:td><xdc:pages>352</xdc:pages></h:td>

     <h:td><xdc:date>1998/01</xdc:date></h:td>

    </h:tr>

   </h:table>

  </xdc:bookreview>

 </h:body>

</h:html>

In this example, the elements prefixed with xdc are associated with a namespace whose name is http://www.xml.com/books, while those prefixed with h are associated with a namespace whose name is http://www.w3.org/HTML/1998/html4.

The prefixes are linked to the full names using the attributes on the top element whose names begin. xmlns:. The prefixes don't mean anything at all - they are just shorthand placeholders for the full names. Those full names, you will have noticed, are URLs, i.e. Web addresses. We'll get back to why that is and what those are the addresses of a bit further on.

Why Namespaces?

But first, an obvious question: why do we need these things? They are there to help computer software do its job. For example, suppose you're a programmer working for XML.com and you want to write a program to look up the books at Amazon.com and make sure the prices are correct. Such lookups are quite easy, once you know the author and the title. The problem, of course, is that this document has XML.com's book-review tags and HTML tags all mixed up together, and you need to be sure that you're finding the book titles, not the HTML page titles.

The way you do this is to write your software to process the contents of <title> tags, but only when they're in the http://www.xml.com/books namespace. This is safe, because programmers who are not working for XML.com are not likely to be using that namespace.

Attributes Too

Attributes, not just elements, can have namespaces. For example, let's use the HTML STYLE attribute to allow an HTML browser to display our book review:


<h:html xmlns:xdc="http://www.xml.com/books"

        xmlns:h="http://www.w3.org/HTML/1998/html4">

 <h:head><h:title>Book Review</h:title></h:head>

 <h:body>

  <xdc:bookreview>

   <xdc:title h:style="font-family: sans-serif;">

     XML: A Primer</xdc:title>

   <h:table>

    <h:tr align="center">

     <h:td>Author</h:td><h:td>Price</h:td>

     <h:td>Pages</h:td><h:td>Date</h:td></h:tr>

    <h:tr align="left">

     <h:td><xdc:author>Simon St. Laurent</xdc:author></h:td>

     <h:td><xdc:price>31.98</xdc:price></h:td>

     <h:td><xdc:pages>352</xdc:pages></h:td>

     <h:td><xdc:date>1998/01</xdc:date></h:td>

    </h:tr>

   </h:table>

  </xdc:bookreview>

 </h:body>

</h:html>

Beautification

That example above is, perhaps, kind of ugly, with all those prefixes and colons clutering up the tags. The Namespaces Recommendation allows you to declare a default namespace and leave out some prefixes, like this:


<html xmlns="http://www.w3.org/HTML/1998/html4"

      xmlns:xdc="http://www.xml.com/books">

 <head><title>Book Review</title></head>

 <:body>

  <xdc:bookreview>

   <xdc:title>XML: A Primer</xdc:title>

   <table>

    <tr align="center">

     <td>Author</td><td>Price</td>

     <td>Pages</td><td>Date</td></tr>

    <tr align="left">

     <td><xdc:author>Simon St. Laurent</xdc:author></td>

     <td><xdc:price>31.98</xdc:price></td>

     <td><xdc:pages>352</xdc:pages></td>

     <td><xdc:date>1998/01</xdc:date></td>

    </tr>

   </table>

  </xdc:bookreview>

 </body>

</html>

In this example, anything without a prefix is assumed to be in the http://www.w3.org/HTML/1998/html4 namespace, which we're using as the namespace name for HTML (presumably, now that namespaces are official, the W3C will give HTML an official namespace name).

What Do Namespace Names Point At?

One of the confusing things about all this is that namespace names are URLs; it's easy to assume that since they're Web addresses, they must be the address of something. They're not; these are URLs, but the namespace draft doesn't care what (if anything) they point at. Think about the example of the XML.com programmer looking for book titles; that works fine without the namespace name pointing at anything.

The reason that the W3C decided to use URLs as namespace names is that they contain domain names (e.g. www.xml.com), which work globally across the Internet.

Is That All There Is?

That's more or less all there is to it. The only purpose of namespaces is to give programmers a helping hand, enabling them to process the tags and attributes they care about and ignore those that don't matter to them.

Quite a few people, after reading earlier drafts of the Namespace Recommendation, decided that namespaces were actually a facility for modular DTDs, or were trying to duplicate the function of SGML's "Architectural Forms". None of these theories are true. The only reason namespaces exist, once again, is to give elements and attributes programmer-friendly names that will be unique across the whole Internet.

Namespaces are a simple, straightforward, unglamorous piece of syntax. But they are crucial for the future of XML programming. Because this is important, we at XML.com will be soon be posting an Annotated Namespaces, in a style similar to our Annotated XML 1.0.