Article
XML Namespaces Explained
Defining Multiple Prefixes for the Same Namespace
It is possible for different prefixes to actually refer to the same namespace, as follows:
<tag>
<foo:head xmlns:foo="http://me.com/namespaces/foofoo">
<foo:title>An example document</foo:title>
</foo:head>
<bar:body xmlns:bar="http://me.com/namespaces/foofoo">
<bar:e1>a simple document</bar:e1>
<bar:e2>
Another element
</bar:e2>
</bar:body>
<tag>
Defining the Same Prefix for Multiple Namespaces
It is also possible (though not recommended) for the same prefix refer to different namespaces, depending on their context:
<myns:html xmlns:myns="http://www.w3c.org/1999/xhtml">
<myns:head>
<myns:title>A really bad idea</myns:title>
</myns:head>
<myns:body>
<myns:h1>A really bad idea</myns:h1>
<myns:pre>
<myns:pre xmlns:myns="http://my.com/namespaces/test-data">
<myns:table>
<myns:data>
Hello World
</myns:data>
</myns:table>
</myns:pre>
</myns:pre>
</myns:body>
Note: This is not a good idea!
Multiple Namespaces
If you're using namespaces, you will almost certainly need to use several namespaces at once -- so how can you declare more than one namespace at a time?
What you do is use more than one xmlns declaration, like this:
<foo:tag xmlns:foo="http://me.com/namespaces/foofoo"
xmlns:bar="http://me.com/namespaces/foobar"
>
<foo:head>
<foo:title>An example document</foo:title>
</foo:head>
<bar:body>
<bar:e1>a simple document</bar:e1>
<bar:e2>
Another element
</bar:e2>
</bar:body>
</foo:tag>
The Default Namespace
Question: If you use any namespaces, do all elements have to exist within a namespace?
Answer: Yes, but this doesn't have to be a problem!
It is permissible to define a namespace that is associated with no prefix -- they're the unqualified names we touched on above.
This is of particular importance for xhtml, as one of the requirements of this language is that xhtml doesn't break HTML -- and HTML doesn't understand prefixes!
To define the default namespace, simply allocate an xmlns with no prefix:
<xhtml xmlns="http://www.w3c.org/1999/xhtml">
For example:
<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:bar="http://me.com/namespaces/foobar"
>
<head>
<title>An example document</title>
</head>
<body>
<bar:e1>a simple document</bar:e1>
<bar:e2>
Another element
</bar:e2>
</body>
</html>