In this post, I will give an overview of what are XML namespaces and why do we need it?
XML Document
...
</root>
...
</abc:root>
The above figure says it all. As we can see that there exists two spaces for our example namely http://techlaks.blogspot.com/rivers and http://techlaks.blogspot.com/finance and each of the space have elements in their respective spaces.
In XML document 1, we want to use elements from both the spaces, so we declare the exact namespaces because it is this URI which binds the elements of domain http://techlaks.blogspot.com/rivers and http://techlaks.blogspot.com/finance in this document. Though we can define any namespace prefix as we wish. In our case, we have chosen abc and def.
Similarly in XML document 2, we wanted to use elements only from the namespace http:techlaks.blogspot.com/rivers, so we declared this namespace in the root element's attribute and chose a namespace prefix riv to get a hold of elements from that namespace in this document.
...
</abc:root>
This is the original XML document which we started off with.
...
</abc:root>
...
</riv:root>
...
</root>
And the above XML document looks like the following if you choose the namespace http://techlaks.blogspot.com/finance as the default namespace
...
</riv:root>
Scenario
Consider a situation where you want to pass a piece of information involving names of banks with different sense.
bank: The land alongside or sloping down to a river or lake
bank: A financial establishment that invests money deposited by customers,
pays it out when required, makes loans at interest, and exchanges...
<?xml version="1.0" encoding="ISO-8859-1"?>
<bank>
...
<root>
...<bank>
<name>Crystal River Bank</name>
</bank>...
<bank>
<name>Standard Chartered Bank</name>
</bank>...
</root>
This document is easy to understand if read by a human, but this is not possible every time in real world where there are lots of documents present and where we use computers to parse an XML document. We need to find a way to distinguish these banks. That is where namespaces come into play.
What are namespaces?
XML namespaces are way to resolve conflict within a document.
As far as syntax is concerned, we put a name prefix before the elements belonging to the same namespace and a xmlns attribute at the start tag of the element. The syntax for this xmlns attribute is
xmlns:<namespace name>="URI"
Let us look how does the above XML document looks in syntactic form.
<?xml version="1.0" encoding="ISO-8859-1"?>
<abc:bank>
...
<abc:root xmlns:abc="http://techlaks.blogspot.com/rivers">
...<abc:bank>
<abc:name>Crystal River Bank</abc:name>
</abc:bank>...
<def:bank xmlns:def="http://techlaks.blogspot.com/finance">
<def:name>Standard Chartered Bank</def:name>
</def:bank>...
</abc:root>
Let us now analyze and understand the syntax and the document
The first line
<abc:root xmlns:abc="http://techlaks.blogspot.com/rivers">
It contains a prefix abc and an attribute xmlns:abc="http://techlaks.blogspot.com/rivers". What does it mean?
First of all let us analyze the attribute first.
It contains a prefix abc and an attribute xmlns:abc="http://techlaks.blogspot.com/rivers". What does it mean?
First of all let us analyze the attribute first.
- xmlns --> This signifies that we will now define an xml namespace (xmlns) for an element
- abc --> This is the namespace prefix by which elements belonging to a particular URI will be referred to in this document.
- http://techlaks.blogspot.com/rivers --> This is the URI (Uniform Resource Identifier) which identifies a set of elements in its domain. Sometimes people consider it to an internet resource. It can be an internet resource as well but in general it is a string which is unique for our purpose and can be anything, may be just rivers. But presenting it in a way similar to a URL is convention followed.
An Analogy
In other words, you can think that there is a space of knowledge which consists of knowledge about root and bank (river sense). That space is identified by "http://techlaks.blogspot.com/rivers" to other people. It says that any XML document which wishes to use the elements of this domain must mention that URI in the root element (the most basic element) as a part of attribute syntax and within that XML document, elements of that space will be identified by the namespace prefix defined by us. Further, when we want to access any element of that domain, we must apply the prefix before that element.The above figure says it all. As we can see that there exists two spaces for our example namely http://techlaks.blogspot.com/rivers and http://techlaks.blogspot.com/finance and each of the space have elements in their respective spaces.
In XML document 1, we want to use elements from both the spaces, so we declare the exact namespaces because it is this URI which binds the elements of domain http://techlaks.blogspot.com/rivers and http://techlaks.blogspot.com/finance in this document. Though we can define any namespace prefix as we wish. In our case, we have chosen abc and def.
Similarly in XML document 2, we wanted to use elements only from the namespace http:techlaks.blogspot.com/rivers, so we declared this namespace in the root element's attribute and chose a namespace prefix riv to get a hold of elements from that namespace in this document.
Equivalent Forms
In general, you won't find only the above mentioned syntax for the XML document. There are other equivalent forms to a single XML document. Let us have a look on those.
Original XML Document
<?xml version="1.0" encoding="ISO-8859-1"?>
<abc:bank>
...
<abc:root xmlns:abc="http://techlaks.blogspot.com/rivers">
...<abc:bank>
<abc:name>Crystal River Bank</abc:name>
</abc:bank>...
<def:bank xmlns:def="http://techlaks.blogspot.com/finance">
<def:name>Standard Chartered Bank</def:name>
</def:bank>...
</abc:root>
This is the original XML document which we started off with.
Equivalent Form 1
Now, instead of defining the namespaces at different points in the document, we can also define all the namespace attributes in root element of the XML document itself. In that case, we don't have to further mention the namespace attribute in the document again. Based on this, you can write the above XML document as below:
<?xml version="1.0" encoding="ISO-8859-1"?>
<abc:bank>
...
<abc:root xmlns:abc="http://techlaks.blogspot.com/rivers"
xmlns:def="http://techlaks.blogspot.com/finance">
...xmlns:def="http://techlaks.blogspot.com/finance">
<abc:bank>
<abc:name>Crystal River Bank</abc:name>
</abc:bank>...
<def:bank>
<def:name>Standard Chartered Bank</def:name>
</def:bank>...
</abc:root>
Equivalent Form 2
Remember, we can define the namespace prefix anything we want as the scope of that namespace prefix is only to this document and can be anything as per your convenience by which you want to access the elements of a namespace. Based on this, below is the XML Docment
<?xml version="1.0" encoding="ISO-8859-1"?>
<riv:bank>
...
<riv:root xmlns:riv="http://techlaks.blogspot.com/rivers"
xmlns:fin="http://techlaks.blogspot.com/finance">
...xmlns:fin="http://techlaks.blogspot.com/finance">
<riv:bank>
<riv:name>Crystal River Bank</riv:name>
</riv:bank>...
<fin:bank>
<fin:name>Standard Chartered Bank</fin:name>
</fin:bank>...
</riv:root>
Equivalent Form 3 - Use of Default Namespaces
Writing namespace prefix for every element in a document takes a lot of time. However, we can avoid this by declaring one of the namespaces declared in the document as the default namespace. A default namespace allows us to not to write the namespace prefix for all the elements belonging to that domain. The syntax is also pretty straight forward. It is
xmlns="URI"
So, for the namespace for which you decide to choose as the default namespace, just do not mention a namespace prefix for that.
So, the above XML document will look like the following if you choose the namespace http://techlaks.blogspot.com/rivers as the default namespace
<?xml version="1.0" encoding="ISO-8859-1"?>
<bank>
...
<root xmlns="http://techlaks.blogspot.com/rivers"
xmlns:fin="http://techlaks.blogspot.com/finance">
...xmlns:fin="http://techlaks.blogspot.com/finance">
<bank>
<name>Crystal River Bank</name>
</bank>...
<fin:bank>
<fin:name>Standard Chartered Bank</fin:name>
</fin:bank>...
</root>
And the above XML document looks like the following if you choose the namespace http://techlaks.blogspot.com/finance as the default namespace
<?xml version="1.0" encoding="ISO-8859-1"?>
<riv:bank>
...
<riv:root xmlns:riv="http://techlaks.blogspot.com/rivers"
xmlns="http://techlaks.blogspot.com/finance">
...xmlns="http://techlaks.blogspot.com/finance">
<riv:bank>
<riv:name>Crystal River Bank</riv:name>
</riv:bank>...
<bank>
<name>Standard Chartered Bank</name>
</bank>...
</riv:root>

No comments:
Post a Comment