XML Namespace is used to avoid element name conflict in XML document.
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:
<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.
<?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:
<table> <tr> <td>Aries</td> <td>Bingo</td> </tr> </table>
Table2: This table carries information about a computer table.
<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.
You can easily avoid the XML namespace by using a name prefix.
<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>
You can use xmlns attribute to define namespace with the following syntax:
<element xmlns:name = "URL">
Let's see the example:
<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.
<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>
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 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:
<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.