Monday, July 9, 2012

XML Namespaces

In this post, I will give an overview of what are XML namespaces and why do we need it?

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 Document

<?xml version="1.0" encoding="ISO-8859-1"?>
<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: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.
  1. xmlns --> This signifies that we will now define an xml namespace (xmlns) for an element
  2. abc --> This is the namespace prefix by which elements belonging to a particular URI will be referred to in this document.
  3. 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.
We have chosen this attribute to be the part of root because as per the syntax, this attribute should be defined in the start tag of the element domain. So, simply speaking, we are saying that the element root and bank (river sense) belongs to a same space or domain or URI and bank (financial sense) belong to another space or domain or URI

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: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:root xmlns:abc="http://techlaks.blogspot.com/rivers" 
               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:root xmlns:riv="http://techlaks.blogspot.com/rivers" 
               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"?>
<root xmlns="http://techlaks.blogspot.com/rivers" 
               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:root xmlns:riv="http://techlaks.blogspot.com/rivers" 
               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>

Tuesday, July 3, 2012

Well Formed XMLs and XML Validation

Well Formed and Valid XML

When we define an XML, we talk two things about it that whether an XML is well formed and whether the given XML is valid. Let us understand briefly what do we mean by that.

Well Formed XML

So, when we talk about an XML being well formed, by that we mean that the XML we are talking about is syntactically correct and meet the following conditions:
  1.  It has necessarily a root element.

    So, the below XML is incorrect because it doesn't contain the root element in it.
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <first>John</first>
    <middle>Dear</middle>
    <last>Jill</last>

    The correct XML is one with a root element
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <name id="10">
        <first>John</first>
        <middle>Dear</middle>
        <last>Jill</last>
    </name>

  2. All the elements in the given XML document have a closing tag

    So, the below element will be incorrect in an XML
    <last>Jill

    The correct XML element format would be
    <last>Jill</last>
    <last />
  3. Make sure that all XML tags match the closing tag (a thing to remember would be that XML tags are case sensitive)

    So, the below element will be incorrect in an XML
    <last>Jill</Last>

    The correct XML element format would be
    <last>Jill</last>
  4. All the elements are properly nested

    So, the below XML nesting is incorrect because it doesn't close tags in order.
    <name id="10">
        <first>John</name>
    </first>


    The correct XML is one with proper sequence of nesting
    <name id="10">
        <first>John</first>
    </name>
  5. All the attributes are quoted

    So, the below XML element is incorrect because it doesn't holds the attribute value in quotes
    <name id=10>
        <first>John</first>
    </name>


    The correct XML is elements containing attribute values in quotes
    <name id="10">
        <first>John</first>
    </name>

 

Valid XML

An XML is valid if it well formed and conforms to a prototype. We already know well formed XMLs from the previous section. What do we mean by conforming to a prototype and at first place why do we need it?
Think of the following scenario, suppose two applications (Application1, Application2) wants to communicate information using a common XML. Suppose that they decided to pass a name using an XML document just as in the above sections.
<?xml version="1.0" encoding="ISO-8859-1"?>
<name id="10">
    <first>John</first>
    <middle>Dear</middle>
    <last>Jill</last>
</name>

 
Now suppose that Application1 finds that a person doesn't have a middle name and it decides to skip that tag information and passes the following information
<?xml version="1.0" encoding="ISO-8859-1"?>
<name id="10">
    <first>John</first>
    <last>Jill</last>
</name>



Application2 on the other side starts decoding the message and expects <middle> tag after <first>, but finds <last> tag, so it fails. Similar kind of misunderstanding can happen many times. So, there is a need of a common structure for the XML instance to which both the application conforms to. In other words, a validation of the XML passed should be done by a common prototype. That is why, we need a validation tool for the XMLs.
XML validation is done by two popular ways: DTD and and XML based alternative to DTD known as XSD. The purpose of both of them is to define a structure to an XML.

XML DTD

The way DTD (Document Type Definition) defines structure for an XML is by listing list of legal elements. So, for our example XML, DTD defintion is as under:
<!DOCTYPE name
[
<!ELEMENT name (first, middle, last)>

<!ATTLIST name id CDATA #REQUIRED>
<!ELEMENT first (#PCDATA)>
<!ELEMENT middle (#PCDATA)>
<!ELEMENT last (#PCDATA)>
 ]>

You can find the W3C recommendation tutorial on this on http://www.w3schools.com/dtd/default.asp

XML Schema

XML Schema is an XML based alternative to DTD to verify an XML document defined by W3C. For our example XML, the schema definition is as under:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="name">

        <xs:complexType>
            <xs:sequence>
                <xs:element name="first" type="xs:string"/>
                <xs:element name="middle" type="xs:string"/>
                <xs:element name="last" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required" />
        </xs:complexType>
    </xs:element>

</xs:schema>

Practical applications

Let me give some practical applications of the validation model.
  • Suppose you have created a web service and want users to use it. You have even decided on protocol or template in XML format on how you will describe your web service. But then you have a question in your mind. Will the users of my web service be able to understand my protocol. You need a common format based on which you can create the web service description and based on that all users of your web service will understand your service details. Based on these requirements, WSDL (Web Services Description Language) was created where the service publisher and receiver follows a common prototype of WSDL
  • Another example could be seen when you use a XML based GUI editor where you mention different layouts and other features for your GUI. The idea there again is to adhere to a common prototype set up by the application framework (say for example Java Swing, Android)

Online References

You can find the W3C recommendation tutorial on this on http://www.w3schools.com/schema/default.asp

Also, you can find several online XML and XSD validators online like the one on  http://xsdvalidation.utilities-online.info

XML Basic Tutorial

This blog is about understanding XML namespaces which we see at most places. Before we do that, let us have a quick review about XML as per W3C recommendation. You can also visit XML Tutorial on the link http://www.w3schools.com/xml/ for a detailed tutorial for XML

What is XML?

The first thing to think about is what is an XML and why do we need it?
Consider a scenario, when we want to pass a piece of structured information from one application to another or from Windows to Linux, wouldn't it be dreamy if there existed a language where structured piece of information could be transmitted easily. That is where XML comes to rescue.
XML stands for Extensible MarkUp Language and is designed to transport data between all sorts of applications. It is a cross platform software and hardware independent tool for information passing.
An XML document is self describing, human readable document with a simple syntax to follow. A simple example of an XML is under

XML Document Example

<?xml version="1.0" encoding="ISO-8859-1"?>
<name id="10">
    <first>John</first>
    <middle>Dear</middle>
    <last>Jill</last>
</name>

Nomenclature

The above document defines a simple piece of name data of a person with his/her first, middle and last name in the document. Let us analyze bits and pieces of this XML document.
 
<?xml version="1.0" encoding="ISO-8859-1"?>
This is the first line in the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/West European character set). There are several other encoding which can be used based on the requirement of the XML document.

<name id="10">
The next line describes the root element of this document and says that this document is about name. Speaking of element, an XML element is everything from (including) the element's start tag to (including) the element's end tag. An element can contain:
  1. another element(s)
  2. a value (text)
  3. attributes
  4. a mixture of above all
An example depicting these is as under:
<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
    <name id="10">
        <first>John</first>
        <middle>Dear</middle>
        <last>Jill</last>
    </name>
    <name id="11">
        <first>Jack</first>
        <middle>Dear</middle>
        <last>Jill</last>
    </name>
</students>

So, we can see that above is a list of students with name elements. The name element has an attribute id and sub elements as first, middle and last

Coming to our original example, the name element also attaches id with itself with value 10 (say it is an identification number of the person). This is the attribute for the name; an attribute can be considered as a metadata or additional info to an element. There can be several of them for an element.

Elements vs Attributes
Additionally, it is a design issue to make a thing as an sub element or an attribute to an element. For example, both the below XMLs are correct and equivalent
<?xml version="1.0" encoding="ISO-8859-1"?>
<name id="10">
    <first>John</first>
    <middle>Dear</middle>
    <last>Jill</last>
</name>

<?xml version="1.0" encoding="ISO-8859-1"?>
<name>
    <id>10</id>
    <first>John</first>
    <middle>Dear</middle>
    <last>Jill</last>
</name>

Returning back to our original example
<first>John</first>
<middle>Dear</middle>
<last>Jill</last>
The next three lines are the child elements of the element name saying that the first name, middle name and the last name are John, Dear and Jill respectively.

So, generally speaking, an XML skeleton can be thought of something like below
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <element>Value</element
    <element> 
            <element>Value</element>
            ... 
    </element>
    .
    ..
    ...
</root>