Article

Home » Client-side Coding » XML, XSLT & Web Services » XML Namespaces Explained

About the Author

Ian Stuart

author_ianstuart1 Ian is a 30-something programmer for the Edina Data Centre at Edinburgh University, where you can visit his site. He's been programming for more years than he cares to remember, and has involved with Web technologies since the early '90's. When he's not at work, he prefers to tinker with old Land Rovers.

View all articles by Ian Stuart...

XML Namespaces Explained

By Ian Stuart

November 27th, 2002

Reader Rating: 8

Page: 1 2 3 Next

Why do we need namespaces?

There are really two fundamental needs for namespaces:

  1. To disambiguate between two elements that happen to share the same name

  2. To group elements relating to a common idea together

OK, so these statements are a bit vague - let's give some examples here:

To disambiguate between elements that happen to share the same name

Consider that:

  • In (x)html there is a table element. There is also an element of the same name in XSL-FO.
  • a, title and style are all elements in both (x)html and SVG.

So, how can you tell an SVG title from an (x)html one?

To group elements relating to a common idea together

In (x)html, the table, style and a elements are governed by specific rules about what is required, and what may and may not be included. The definitions required by these rules should all be included in the same place.

So, for example, my own XML-based data may have validating rules, and I will want to:

  • define those rules in the same place, and
  • differentiate these particular rules from any other rule-set that I (or someone else) define.

What is a Namespace?

A namespace is a unique URI (Uniform Resource Locator)

The advantage of this format is that anyone who transmits XML can be assumed to have access to a domain name (the bit after the http://, but before the next /). It's bad form to piggy-back on someone else's domain (especially if they don't know you're doing it!).

In an XML document, the URI is associated with a prefix, and this prefix is used with each element to indicate to which namespace the element belongs. For example,

rdf:description  
xsl:template  
zblsa:data

In these examples,

  • the part before the colon is the prefix
  • the part after the colon is the local part
  • any prefixed element is a qualified name
  • any un-prefixed element is an unqualified name

How do I use a Namespace?

To use a namespace, you first associate the URI with a namespace:

<foo:tag xmlns:foo="http://me.com/namespaces/foofoo">.

This defines foo as the prefix for the namespace for that element tag. The attribute prefixed with xmlns works like a command to say "link the following letters to a URI". As no well-formed document can contain two identical attributes, the part that appears after the colon stops the same prefix being defined twice simultaneously.

Defining One Prefix for a Namespace

Here's an example where we define one prefix for a namespace:

<foo:tag xmlns:foo="http://me.com/namespaces/foofoo">

 <foo:head>
   <foo:title>An example document</foo:title>
 </foo:head>

 <foo:body>
   <foo:e1>a simple document</foo:e1>
   <foo:e2>
     Another element
   </foo:e2>
 </foo:body>
</foo:tag>

For all elements within <foo:tag>, the namespace prefix foo is associated with the namespace URI http://me.com/namespaces/foofoo.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links