Monday, February 5, 2007

XML and XSLT - A small Overview

XML...

what is XML? xml is a markup laguage like html. we can say..,we can represent data
in whatever format we want with XML.

You can use youe own tags instead fixed ones like in html.

TAG: tag is like, <me name="pg">abcdefg</me>.

Here, <me> is called a tag (also an element) and 'name' is called an attribute of element.
And the value written inside.User can represent data with such elements.


for example:
<root>
<username>afsdf</username>
<pwd>asdasd</pwd>
</root>



Here root element is parent of all. there are two children nodes inside it.
Today, in programming, user can use xml files for configuration of programmes.
He can store data inside xml file which is used at runtime.
He can put some settings here which are to be changed for settings.
He can change then easily like an property file.


XSL...

what is XSL? XSL is the stylsheet language used to format xml data as per user's need. Means he can convert one xml file to another XML,HTML etc. He can access the value of any element or node inside xml file.

to get value from any node through xsl file:
<h4><xsl:value-of select="root/username"></xsl:value-of></h4>

to get value from any node's attribute through xsl file:
<h4><xsl:value-of select="me/@name"></xsl:value-of></h4>

so here he will get value of <username> tag in <h4> tag of html. Now he can store the output file as html or xml.

now i want to talk about some special features.

XPATH:here the value given inside "select" attribute of "<xsl:value-of>" is called a XPATH.

user can have variables,for-each loop, if and when conditions in XSLT language.
user can also make functions using "<xsl:template>" tag of XSLT and then call the template in any element of XSL stylesheet.

if statement:
<xsl:if test="me/@name = 'pg'"></xsl:if>
<xsl:value-of select="."></xsl:value-of>


simplified if statement if the node is inside me element:
<xsl:value-of select="me[@name='pg']"></xsl:value-of>

both the statement above will select value of if the attribute name is equal to 'pg'.
first one is simple if statement which can be used anywhere.
second one is simplified version but it can be only used if the node or attibute which is to be selected is child or attribute of the node in "test" condition of "<xsl:if>"

There is a long list of functions for string,number etc. we will see more about xslt in next tutorial.

just use input xml and xsl files in xmltransformer provided in you programming language package and you will get output of the transformation..NET and JAVA provides Transformer for XML.

And if you want to test your xsl before then you can use:

1.Altova XMLSpy Hpme Edition - which is free but may be not now.
2.Cooktop - which is freeware - goood.
3.Xselerator - which is paid but the best.

you can find more on XML and XSLT on
1.DevGuru
2.w3schools

0 Comments: