Wednesday, 1 December 2010

Role of namespaces in XML

What is the role of namespaces in XML?  What benefits does the use of namespaces confer and what problems may be avoided?  Give brief examples to illustrate your answers.

# Namespaces are a simple and clear-cut way to distinguish names used in XML documents. It is an abstract environment created to hold a logical grouping of unique identifiers or symbols. Basically, it is used for providing uniquely names elements and attributes in an XML document. There can be many element or attributes names in an XML from more than one XML vocabulary. So, if namespace is assigned to each vocabulary then the ambiguity between identically named elements or attributes can be resolved.
XML namespaces are declared with an xmlns attribute, which can correlate a prefix with the namespace. The declaration is in scope for the element holding the attribute and all its descendants. For example:
<! -- Declares two XML namespaces. Their scope is the A and B elements. -->
<A xmlns:good="http://www.good.org/" xmlns="http://www.bad.org/">
<B>Kshitij</B>
</A>
If an XML namespace declaration contains a prefix, element type and attribute names have to be referred in that namespace with the prefix. For example:
<! -- A and B are in the http://www.good.org/ namespace,
which is associated with the good prefix. -->
<good:A xmlns:good="http://www.good.org/">
<good:B>Kshitij</good:B>
</good:A>
If its declaration does not contain a prefix, the namespace is the default XML namespace and element type names have to be referred in that namespace without a prefix. For example:
<! -- This is equivalent to the previous example but uses
a default namespace instead of the good prefix. -->
<A xmlns="http://www.good.org/">
<B>Kshitij</B>
</A>
In fact, XML namespaces were designed to give universally unique names for an elements and attributes. In tradition, they are used in XML technologies when unique names are needed, like for difficult type names in XML schemas and functions name in XQuery. Considering the following two XML documents for demonstrating why XML namespaces are needed:
1.
<? xml version=”1.0” ?>
<Address>
               <Name> Middlesex University </Name>
               <Town> Hendon </Town>
               <County> Middlesex </County>
               <PostCode> NW4 4BT </PostCode>
</ Address >
2.
<? xml version=”1.0” ?>
<Server>
               <Address>123.45.67.8</Address>
</Server>
Both documents used a different XML language and each language defines an Address element. Though it has same element both had different content model, a different meaning and is interpreted by an application in a different way. It’s not an issue as long as these element types exist only in separate documents. But if it is use in same documents then an application won’t know which element type it is processing. One solution for this is simply renaming one of the elements types such as if there is two Address we can change the second one to IPAddress. However, this is not a useful long term solution. So a better solution is to assign each language (including its Address element type) to a different namespace, which will allow to continue using the Address name in each language, but to distinguish between the two different elements types. And the mechanism by which it is solved is XML namespaces. For example:
 
<University>
               <Name> Middlesex University </Name>
               <addr:Address xmlns:addr=”http://www.mdx/address”>
               <addr:Town> Hendon </addr:Town>
               < addr:County> Middlesex </ addr:County>
               < addr:PostCode> NW4 4BT </ addr:PostCode>
               <addr:Address>
               <serv:Server xmlns:serv="http://www.mdx/server">
        <serv:Address>123.45.67.8</serv:Address>
               </serv:Server>
  </University>
The first Address element type name belongs to the http://www.mdx/address XML namespace. It has an expanded (two-part) name of “http://www.mdx/address” plus "Address" and we write it as {http://www.mdx/address}Address. The second Address element type name belongs to the http://www.mdx/server XML namespace and has an expanded name of {http://www.mdx/server }Address. Thus, each expanded name is unique, meeting the requirement that each element type in an XML document have a unique name.

No comments:

Post a Comment