Sunday, September 26, 2010

XML SCRIPTING

XML Language can be understood as a generic language used to describe other markup languages. You need to understand that XML makes a clear distinction between the markup and the content of the webpage. Here markup implies tags and attributes that are being used in the XML document and content refers to the information being presented in the document.

E.g.
< p > XML is used to store data in a structured way < /p >

In this example < p >…. </p > refers to the markup being used in the document and the text written between these tags refer to the content of the document.

You can say that markup is actually used to describe the presentation of the content. This is done using standard tags and attributes that are available in HTML. You will find that the XML markup is generally used to describe the content of the document and is not related with the appearance of the document.

E.g. < quiz answer=”Qutab Minar” > Can you name a famous monument in delhi?< /quiz >

In this example < quiz > tag is being used to describe the type of content and the answer attribute specifies the answer for this question.

To start using XML effectively you need to learn about the terminology used in XML and understand the structure of a XML file. Consider following example:

< catalog >
< movie >
< title > Jung < >
< duration > 3 hrs < /duration >
< /movie >
< /catalog >

As you can see XML files have hierarchical structure. Each tag used in XML defines an element. Each element defined should have an opening as well as a closing tag. E.g. < catalog > has opening as well as closing tag. You will find that some of the elements are self-contained. You do not need to enclose any information in them. These tags can be considered empty element. Such tags can be made self-closing by adding "/ > >" at the end of the opening tag. The hierarchical structure enables easy parsing of the document. As in above example catalogue contains information about movie which ultimately contains detail about title and duration of the movie.

XML Syntax

Check out the following example:

Line 1: < ?xml version="1.0" ? >
Line 2: < library >
Line 3: < type="Operational Research" year="1992" >
Line 4: < book1 > Linear Programming < /book >
Line 5: < book2 > Non Linear Programming < /book >
Line 6: < book3 > Mathematical Programming < /book >
Line 7: < /book >
Line 8: < /library >

The first line is a processing Instruction. Processing Instruction is used to define the XML version of the document. From Line 1 you will find that the example written conforms to the 1.0 specification of XML:
< ?xml version=”1.0”? >

2 defines the first element of the document which is the root element:
< library >
next lines define child elements of the root i.e. Book which further has child elements (book1, book2, book3).

You can see that an XML documents use a self-describing syntax which is very simple to understand. Before you read further about xml scripting you need to be aware of the major components of an xml document. XML mark-up document can be broadly divided into a set of components which describe the makeup of a XML document. These components can be defined as follows:

1. Element Tag: An element can be understood as a piece of information that corresponds to a tag or a set of tags in a XML document. In other words element can be understood as a logical piece of markup that is represented as a tag in a XML document. E.g. In above example ‘quiz’ is an element which has been used as < quiz > &lgt; /quiz > tag in the document.


Note that an element need to have both starting and ending tags like < quiz > …< /quiz > ,< p > …< /p > or a simple empty tag like < img/ >. While coding in HTML empty tag < br > do not need to have end tag. However with XML be careful you need to close every tag.

2. Processing Instruction: Apart from markup and content you will find processing instructions written in a XML document which is the first statement in the document. A processing instruction can be understood as a special command passed along to the program which will process the document. Processing instruction written in < ?.....? > .
E.g. < ?xml version="1.0"? >

This processing instruction is the first statement of a XML document. You will find that the processing instruction is similar to a tag. It includes name and attribute/value pair. This processing instruction tells that the document adheres to the standard of xml version 1.0.

3. Comments in XML:

In a XML document comments can be written using following syntax:

< !-- In this document you are learning about xml -->

Note: You can write comments in XML in the same way as you write in HTML.

4. Document Type Declaration: It is used for describing the structure of an XML document. It identifies the external DTD that defines the structure of an xml document. The external DTD( DTD stands for Document type definition) is created for describing the structure of the xml document. You need to put the ‘Document Type declaration’ on the top of the xml document. It is written just below the processing instruction. Its use is to perform three basic tasks:

1. Document Type Declaration is used to identify the root element of the document. In an xml document there is a root element such that all other elements are the children of the root element.

2. Identifies the external DTD of the file. An xml file is created according to the document structure defined in the DTD.

E.g. Check out the XML below which describes audio/video collection

< ? xml version="1.0"? >
< !DOCTYPE entertainment SYSTEM entertainment.dtd >
< entertainment >
< Audio >
< track1 > Tara Rampam < >
< track2 > Let's go for party < /track2 >
< /Audio >
< Video >
< track1 > Jumanji < /track1 >
< track2 > Home Alome < /track2 >
< /Video >
< /entertainment >

In above example first line is a processing instruction which shows that this document should be processed according to the xml version 1.0 standards. In second line is the document type declaration which states that the root element for this xml file is ‘entertainment’. Further it identifies that the document need to be verified according to the external DTD namely “entertainment.dtd”. While processing this file browser needs to look for “entertainment.dtd” and then validate the document structure according to this file.

No comments:

Post a Comment