Namespaces in XML

The purpose of this assignment was to:

The description and my solution of the Namespaces in XML - assignment can be viewed at the bottom of this page.

What are XML Namespaces?

Namespaces in XML is a mechanism for qualifying element and attribute names used in XML documents by associating them with contexts/namespaces identified by URI references.

What are a URIs and why use them to identify XML Namespaces?

An Uniform Resource Identifier (URI) is a string of characters which identifies resources on the Internet. The most common sort of (subset of) URI is the Uniform Resource Locator (URL), which identifies a domain address on the Internet.

The URIs used for identifing namespaces are not supposed to point to anything. A XML parser does not use them to look up information.

Using URIs is simply a way of getting globally unique names.

Why use XML Namespaces?

A single XML document may contain elements and attributes ("a markup vocabulary") that are defined for, and used by, multiple software modules.

If a markup vocabulary(elements and attributes) exists which is well understood, and for which there is useful software available, it's better to reuse this markup vocabulary rather than reinvent it.

Such documents containing multiple markup vocabularies pose problems of recognition and collision. Software modules need to be able to recognize the elements and attributes which they are designed to process, even in the face of "collisions" occurring when a markup vocabulary intended for some other software packages(i.e. modules) uses the same element or attribute names.

These considerations require that document constructs should have names constructed so as to avoid collisions between names from different markup vocabularies.

A common way of avoiding collisions is a mechanism called XML namespaces, which accomplishes this by assigning expanded names to elements and attributes.

XML Namespaces Example

Let's asume I would like to describe the Web tutorials contained on this website. To have the tutorials displayed in a nice way I could use XHTML which has this namespace defined:

Furthermore, I might want the descriptions marked up with XML to make them easily accessible by software. I could for instance come up with this namespace:

I could then use the 2 namespaces like this:

<x:html xmlns:tut="" xmlns:x=""> <x:head><x:title>Tutorial Description</x:title></x:head> <x:body> <tut:tutorial_description tut:id="7"> <tut:title x:id="heading">XHTML 1.1 Tutorial</tut:title> <x:table> <x:tr> <x:td>Author</x:td><x:td>Link</x:td> </x:tr> <x:tr> <x:td><tut:author>Martin Carlsson</tut:author></x:td> <x:td> <tut:link> xhtml-1-1-tutorial/ </tut:link> </x:td> </x:tr> </x:table> </tut:tutorial_description> </x:body> </x:html>

The elements and attributes prefixed with tut are associated with a namespace whose name is

The elements and attributes prefixed with x are associated with a namespace whose name is

The prefixes are linked to the full names using the xmlns: (XML NameSpace) attribute on the top element(also called the root element). The prefixes themselfes don't mean anything, they are simply shorthand placeholders for the full names.

Note that the URLs used for identifing these namespaces are not used by a XML parser to look up information. They are treated as strings of characters, not addresses. Their only purpose is to give namespaces unique names.

Often, however, namespaces URIs are used as - besides their main purpose of indentifing namespaces - pointers to real Web pages containing information about the namespaces. This, however, has noting to do with namespaces mechanism. doesn't go anywhere but a 404(not found) page, and go to a actual Web page giving some information about the XHTML namespace.

It's up to the author of the XML docuemnt to decide what - if anything - the namespace should point to.

More on why XML Namespaces are useful

Asume you're a programmer and want to write a program that receives the the title and id in the example above. The problem without namespaces would be to distinguish the tutorial_description tags and attributes from the XHTML tags and attributes, since the title tag and the id attribute are used for both XHTML markup and tutorial_description markup.

This is, however, not a problem if namespaces are used, because then you simply write software to process the contents of title tags and id attributes only when they're in the namespace.

Default XML Namespace

Since it can get kind of messy with all those prefixes you can declare a default namespace instead, like this:

<html xmlns="" xmlns:tut=""> <head><title>Tutorial Description</title></head> <body> <tut:tutorial_description tut:id="7"> <tut:title id="heading">XHTML 1_1 Tutorial</tut:title> <table> <tr> <td>Author</td><td>Link</td> </tr> <tr> <td><tut:author>Martin Carlsson</tut:author></td> <td> <tut:link> web-tutorials/xhtml-1-1-tutorial/ </tut:link> </td> </tr> </table> </tut:tutorial-description> </body> </html>

In this example, any element or attribute without a prefix is associated with the namespace.

Assignment Description

Change the CV from XML Basics -assignment to use at least two namespaces. No DTD is needed.

My solution, Assignment Files

I've extended the CV from XML Basics -assignment with a the copyright element. For this element I'm using a shorthand placeholder/prefix named rights which is linked to this namespace:

I use a default namespace - - for all non-prefixed elements and attributes.

«  Previous Next  »