Home >>XML Tutorial >XML Namespaces
XML Namespace is included in XML document to prevent conflicting element names.
A Namespace is a set of unique names. Namespace is a mechanism by which a group can be assigned the name of element and attribute. URI(Uniform Object Identifiers) defines the Namespace.
Using the reserved XML attribute a namespace for XML is defined. This name attribute must begin with "xmlns."
Let's see the XML namespace syntax:
<element xmlns:name = "URL">
Syntax
Name space here starts with "xmlns" keyword. The term name is a prefix for namespace. The URL is an object for namespace.
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>suresh</cont:name>
<cont:company>phptpoint</cont:company>
<cont:phone>(0120) 8720945</cont:phone>
</cont:contact>
Namespace Prefix:cont
Namespace Identifier: http://sssit.org/contact-us
It specifies that the name of the function and the names of the attribute with the cont prefix belong to the namespace http:/sssit.org/contact-us.
In XML, the developer defines the name of the elements, so that there is a chance of conflicting in the name of the elements. We use XML Namespaces to prevent such kinds of confliction. We may say XML Namespaces offers a way to prevent conflicting element names.
This conflict generally arises when we try to mix XML documents from different XML applications.
Let's take an example with two tables:
Table1:
<table>
<tr>
<td>phptpoint</td>
<td>Noida</td>
</tr>
</table>
Table2:
This table carries information about a computer table.
<table>
<name>PHP </name>
<width>85</width>
<length>100</length>
</table>
When you put these two XML fragments together, a name dispute will arise as they all have <table> element. We have different names and definitions although they do.
Using a name prefix you can easily avoid namespace in XML.
<h:table>
<h:tr>
<h:td>phptpoint</h:td>
<h:td>Noida</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>PHP</f:name>
<f:width>85</f:width>
<f:length>100</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>phptpoint</h:td>
<h:td>Noida</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.xyz.com/furniture">
<f:name>PHP</f:name>
<f:width>85</f:width>
<f:length>100</f:length>
</f:table>
</root>
Throughout the above example, the <table> element specifies a namespace and the child elements with the same prefixes are identified with the same namespace when a namspace is specified for an element.
<root xmlns:h="http://www.abc.com/TR/html4/"
xmlns:f="http://www.xyz.com/furniture">
<h:table>
<h:tr>
<h:td>phptpoint</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 resource on the Internet. It is a collection of characters.
The most popular URI is the URL (Uniform Resource Locator) that defines an address within the internet domain.
There is also a URN (Universal Resource Name) name for the URI but it is not so popular. In all our explanations we used just URL's.
In the XML document the default namespace is used to prevent you from the usage of prefixes in all child elements.
The main distinction between default namespace and basic namespace is that: In default namespace, there is no need to use a prefix.
You can also use multiple namespaces just define a namespace against a child node within the same document.
Example of Default Namespace:
<tutorials xmlns="http://www.phptpoint.com/php">
<tutorial>
<title>php</title>
<author>phptpoint</author>
</tutorial>
</tutorials>
You can see that prefix is not used in this example, so it is a default namespace.