Use XSLT for Transformation of XML into HTML
Use XSLT for Transformation
Usage
XSL stands for EXtensible Stylesheet Language. It is a style sheet language for XML documents.
XSLT means for transforming XML files using XSL.
Before going continue, I presume that you might be familiar with HTML, XML, XPath etc.
As I said above, XSLT is used to transform XML files into other formats like HTML or any other format which can be understand by browsers. Using XSLT, we can search, sort, rearrange, put conditional checks (if) and loop through (foreach) XML elements. In this article, I will a sample XML and XSLT file to transform it to HTML.
Before moving ahead, let's take a look at the elements and methods of XSLT. I have not covered here everything but the most useful elements and methods.
Elements
comment -> is used to create a comment node in the result tree
copy -> is used to copy the current node without child nodes and their attributes
copy-of -> is used to copy the current node with child nodes and their attributes
for-each -> is used to iterate through each node in the current node set
if -> to put a conditional check
sort -> is used to sort the result tree
text -> is used to write literal text
transform -> is used to define the root element of the stylesheet
value-of -> is used to get the value of the selected node
variable -> is used to declare a global or local variable
Methods
current() -> is used to get the current node
format-number() -> is used to convert a number to a stringExample
Sample XML file
I have taken a simple xml file to give an example of using XSLT.
BookStore.xml
< ?xml version="1.0" encoding="ISO-8859-1" ? >
< BookStore >
< Book >
< Type >Comics< /Type >
< Title >Nagraj< /Title >
< Company >Raj Comics< /Company >
< Author >Raj< /Author >
< PublicationDate >01-Jan-2008< /PublicationDate >
< Price >35< /Price >
< /Book >
< Book >
< Type >Magzine< /Type >
< Title >StarDust< /Title >
< Company >StartDust Co.< /Company >
< Author >Multiple< /Author >
< PublicationDate >05-Feb-2008< /PublicationDate >
< Price >120< /Price >
< /Book >
< Book >
< Type >NewsPaper< /Type >
< Title >The Times of India< /Title >
< Company >Times Group< /Company >
< Author >Multiple< /Author >
< PublicationDate >01-Jan-2008< /PublicationDate >
< Price >5< /Price >
< /Book >
< Book >
< Type >Comics< /Type >
< Title >Billu and Pinky< /Title >
< Company >Diamond Comics< /Company >
< Author >Pran< /Author >
< PublicationDate >01-Mar-2008< /PublicationDate >
< Price >50< /Price >
< /Book >
< /BookStore >XSLT
Here is the XSLT to transform the above xml file into simple HTML.
< ?xml version="1.0" encoding="utf-8"? >
< xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" >
< xsl:output method="html" indent="yes"/ >
< xsl:template match="/" >
< html >
< body >
< table border="1px" >
< tr >
< td colspan="6" align="center" >
< h2 >BookStore< /h2 >
< /td >
< /tr >
< tr >
< th >Book Type< /th >
< th >Company< /th >
< th >Title< /th >
< th >Author< /th >
< th >Publish Date< /th >
< th >Price< /th >
< /tr >
< xsl:for-each select="/BookStore/Book" >
< tr >
< td >
< xsl:value-of select="Type"/ >
< /td >
< td >
< xsl:value-of select="Company"/ >
< /td >
< td >
< xsl:value-of select="Title"/ >
< /td >
< td >
< xsl:value-of select="Author"/ >
< /td >
< td >
< xsl:value-of select="PublicationDate"/ >
< /td >
< td align="right" >
< xsl:value-of select="Price"/ >
< /td >
< /tr >
< /xsl:for-each >
< /table >
< /body >
< /html >
< /xsl:template >
< /xsl:stylesheet >
I have used for-each and value-of elements in the XSLT. for-each element is used to iterate through each node found for specified template match (
The output would be a simple table showing all the records with columns specified in the XSLT.
I hope that the above example and explanation is good enough to start using XSLT. I will cover more options like if element into my next articles. Any query, doubt, correction are most welcome.
Umesh M

Since, XML and XSLT can't be seen, I have attached files here.