XML Namespaces

XML Namespace is used to avoid element name conflict in XML document.

XML Namespace Declaration

An XML namespace is declared using the reserved XML attribute. This attribute name must be started with "xmlns".

Let's see the XML namespace syntax:

snippet
<element xmlns:name = "URL">

Here, namespace starts with keyword "xmlns". The word name is a namespace prefix. The URL is a namespace identifier.

Let's see the example of XML file.

snippet
<?xml version="1.0" encoding="UTF-8"?>
<cont:contact xmlns:cont="http://sssit.org/contact-us">
   <cont:name>Vimal Jaiswal</cont:name>
   <cont:company>SSSIT.org</cont:company>
   <cont:phone>(0120) 425-6464</cont:phone>
</cont:contact>

Namespace Prefix: cont

Namespace Identifier: http://sssit.org/contact-us

It specifies that the element name and attribute names with cont prefix belongs to http://sssit.org/contact-us name space.

In XML, elements name are defined by the developer so there is a chance to conflict in name of the elements. To avoid these types of confliction we use XML Namespaces. We can say that XML Namespaces provide a method to avoid element name conflict.

Generally these conflict occurs when we try to mix XML documents from different XML application.

Let's take an example with two tables:

Table1:

snippet
<table>
  <tr>
    <td>Aries</td>
    <td>Bingo</td>
  </tr>
</table>

Table2: This table carries information about a computer table.

snippet
<table>
  <name>Computer table</name>
  <width>80</width>
  <length>120</length>
</table>

If you add these both XML fragments together, there would be a name conflict because both have <table< element. Although they have different name and meaning.

How to get rid of name conflict?

1) By Using a Prefix

You can easily avoid the XML namespace by using a name prefix.

snippet
<h:table>
  <h:tr>
    <h:td>Aries</h:td>
    <h:td>Bingo</h:td>
  </h:tr>
</h:table>
<f:table>
  <f:name>Computer table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
Note
Note: In this example, you will get no conflict because both the tables have specific names.

2) By Using xmlns Attribute

You can use xmlns attribute to define namespace with the following syntax:

snippet
<element xmlns:name = "URL">

Let's see the example:

snippet
<root>
<h:table xmlns:h="http://www.abc.com/TR/html4/">
  <h:tr>
    <h:td>Aries</h:td>
    <h:td>Bingo</h:td>
  </h:tr>
</h:table>

<f:table xmlns:f="http://www.xyz.com/furniture">
  <f:name>Computer table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
</root>

In the above example, the <table> element defines a namespace and when a namespace is defined for an element, the child elements with the same prefixes are associated with the same namespace.

snippet
<root xmlns:h="http://www.abc.com/TR/html4/"
xmlns:f="http://www.xyz.com/furniture">
<h:table>
  <h:tr>
    <h:td>Aries</h:td>
    <h:td>Bingo</h:td>
  </h:tr>
</h:table>
<f:table>
  <f:name>Computer table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
</root>
Note
Note: The Namespace URI used in the above example is not necessary at all. It is not used by parser to look up information. It is only used to provide a unique name to the Namespace identifier.

Uniform Resource Identifier (URI)

Uniform Resource Identifier is used to identify the internet resource. It is a string of characters.

The most common URI is URL (Uniform Resource Locator) which identifies an internet domain address.

There is also an URI name URN (Universal Resource Name) but it is not so common. We have used only URL's in all our examples.

The Default Namespace

The default namespace is used in the XML document to save you from using prefixes in all the child elements.

The only difference between default namespace and a simple namespace is that: There is no need to use a prefix in default namespace.

You can also use multiple namespaces within the same document just define a namespace against a child node.

Example of Default Namespace:

snippet
<tutorials xmlns="http://www.rookienerd.com/java-tutorial">
  <tutorial>
    <title>Java-tutorial</title>
    <author>Sonoo Jaiswal</author>
  </tutorial>
  ...
</tutorials>

You can see that prefix is not used in this example, so it is a default namespace.

Note
Note: If you define a namespace without a prefix, all descendant elements are considered to belong to that namespace.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +