Tuesday, July 3, 2012

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>

No comments:

Post a Comment