XML Tutorial

XML namespace

Introduction

Since XML allows designers to chose their own tag names, it is possible that two or more designers may choose the same tag names for some or all of their elements. XML namespace solves this problem. It provides a way to distinguish between XML elements that have the same local name but are, in fact, from different vocabularies. This is done by associating an element with a namespace. A namespace acts as scope for all elements associated with it.

Syntax

<prefix:localname xmlns:prefix='namespace URI'/>
<prefix:localname xmlns:prefix='namespace URI'></prefix:localname/>
<prefix:localname xmlns:prefix='namespace URI'>children</prefix:localname/>

This XML contains a list of tutorials for client side tools of web development:


<?xml version="1.0" ?>
<tutorial>
<name>html</name>
<name>css</name>
<name>javascript</name>
</tutorial>

This XML contains a list of tutorials for server side tools of web development:


<?xml version="1.0" ?>
<tutorial>
<name>php</name>
<name>asp</name>
<name>jsp</name>
</tutorial>;

If these two XML instances are put together, since both of them carry elements with tagname name, it will create a naming collision. An XML parser will not be able to understand the meaning of using tutorial element is actually for different purposes. Using namespace, this problem can be solved:


<?xml version="1.0" ?>
<cl:tutorial xmlns:cl="http://www.w3resource.com/client-side-tutorials">
<cl:name>html</cl:name>
<cl:name>css</cl:name>
<cl:name>javascript</cl:name>
</cl:tutorial>
<srv:tutorial xmlns:srv="http://www.w3resource.com/server-side-tutorials">
<srv:name>php</srv:name>
<srv:name>asp</srv:name>
<srv:name>jsp</srv:name>
</srv:tutorial>

Namespaces themselves also have names. A namespace name is a uniform resource identifier (URI). Such a URI serves as a unique string. The namespace name and local name of the element together form a globally unique name known as a qualified name.
In this example, we have shown the use of a qualified name.


<?xml version="1.0" ?>
<srv:tutorial xmlns:srv='urn: 
http://www.w3resource.com/server-side-tutorial'>
<name>php</name>
<name>asp</name>
<name>jsp</name>
</srv:tutorial>

This is another example of using qualified elements using a default namespace declaration:


<?xml version="1.0" ?>
<tutorial xmlns:srv='urn: 
http://www.w3resource.com/server-side-tutorial'>
<name>php</name>
<name>asp</name>
<name>jsp</name>
</tutorial>

Namespace declarations appear inside an element start and end tag and are used to map a namespace prefix. The syntax for a namespace declaration is xmlns: prefix="URI".

It is also possible to map a namespace name to no prefix using a default namespace declaration. The syntax for a default namespace declaration is xmlns="URI".

In both cases, the URI may appear in single or double quotes. Only one default namespace declaration may appear on an element. Any number of non-default namespace declarations may appear on an element, provided they all have different prefix parts. It is legal, although not particularly useful, to map the same URI to more than one prefix.

All namespace declarations have a scope - that is, a set of elements to which they may apply. A namespace declaration is in scope for the element on which it is declared and all of that element's descendants. The in-scope mapping of a given prefix to a namespace name can be overridden by providing a new mapping for that prefix on a descendant element. The in-scope default namespace can be overridden by providing a new default namespace declaration on a descendant element.

The names of all elements in a document that conforms to the Namespaces in the XML namespace specification are QNames. Syntactically, all QNames have a local name and an optional prefix.

Example of QNames:

Both the local name and the prefix are NCNames. An NCName is named without a colon in it. The syntax for an element with a prefix is the prefix, followed by a colon, followed by the local name. The namespace of an element with a given prefix is the namespace specified by the in-scope namespace declaration for that prefix. It is an error if no such namespace declaration is in scope. The namespace of un-prefixed elements is the namespace specified by the in-scope default namespace declaration, if any. If no default namespace declaration is in scope, then such elements are not in any namespace. Elements not in any namespace are known as unqualified elements. The namespace name of unqualified elements is the empty string " ". If a default namespace declaration is in scope and an unqualified element is required, the default namespace declaration can be masked by providing a namespace declaration of the form xmlns=' ' on the element.