Example to show how XSLT is used to convert XML into HTML

Consider the below xml,  Sample.xml

<?xml version="1.0"?>

<?xml-stylesheet type="text/xsl" href="total.xsl" ?><!-- Link the XSL Style Sheet to the XML                                                          Document-->

<collection>
<book>
<title>Unearth</title>
<year>1974</year>
<publisher>Living Hand</publisher>
<note>early Auster poetry - out of print</note>
</book>
 
<book>
<title>White Spaces</title>
<year>1980</year>
<publisher>Station Hill</publisher>
<note>out of print</note>
<amazon>http://www.amazon.com/exec/obidos/ASIN/0930794273/qid=1021948543/sr=1-4/ref=sr_1_4/102-2599247-6584128</amazon>
</book>
 
<book>
<title>The Invention of Solitude</title>
<year>1982</year>
<publisher>Sun Press</publisher>
<note>My favorite book</note>
<amazon price="10.36">http://www.amazon.com/exec/obidos/ASIN/0140106286/qid=1021947570/sr=1-14/ref=sr_1_14/102-2599247-6584128</amazon>
</book>
 
<book>
<title>Squeeze Play: A Novel</title>
<year>1982</year>
<publisher>Alpha-Omega</publisher>
<note>published under the name Paul Benjamin - out of print</note>
<amazon>http://www.amazon.com/exec/obidos/ASIN/0940941015/qid=1021948399/sr=1-18/ref=sr_1_18/102-2599247-6584128</amazon>
</book>
 
<book>
<title>In the Country of Last Things </title>
<year>1987</year>
<publisher>Viking</publisher>
<amazon price="10.36">http://www.amazon.com/exec/obidos/ASIN/0140097058/qid=1021947570/sr=1-20/ref=sr_1_20/102-2599247-6584128</amazon>
</book>
 
<book>
<title>The Music of Chance</title>
<year>1990</year>
<publisher>Viking</publisher>
<amazon price="10.36">http://www.amazon.com/exec/obidos/ASIN/0140154078/ref=pd_bxgy_text_1/102-2599247-6584128</amazon>
</book>
 
<book>
<title>The New York Trilogy</title>
<year>1990</year>
<publisher>Penguin Books</publisher>
<amazon price="10.47">http://www.amazon.com/exec/obidos/ASIN/0140131558/qid=1021947332/sr=2-1/ref=sr_2_1/102-2599247-6584128</amazon>
</book>
</collection>

 

 

 
<book>
<title>White Spaces</title>
<year>1980</year>
<publisher>Station Hill</publisher>
<note>out of print</note>
<amazon>http://www.amazon.com/exec/obidos/ASIN/0930794273/qid=1021948543/sr=1-4/ref=sr_1_4/102-2599247-6584128</amazon>
</book>
 
<book>
<title>The Invention of Solitude</title>
<year>1982</year>
<publisher>Sun Press</publisher>
<note>My favorite book</note>
<amazon price="10.36">http://www.amazon.com/exec/obidos/ASIN/0140106286/qid=1021947570/sr=1-14/ref=sr_1_14/102-2599247-6584128</amazon>
</book>
 
<book>
<title>Squeeze Play: A Novel</title>
<year>1982</year>
<publisher>Alpha-Omega</publisher>
<note>published under the name Paul Benjamin - out of print</note>
<amazon>http://www.amazon.com/exec/obidos/ASIN/0940941015/qid=1021948399/sr=1-18/ref=sr_1_18/102-2599247-6584128</amazon>
</book>
 
<book>
<title>In the Country of Last Things </title>
<year>1987</year>
<publisher>Viking</publisher>
<amazon price="10.36">http://www.amazon.com/exec/obidos/ASIN/0140097058/qid=1021947570/sr=1-20/ref=sr_1_20/102-2599247-6584128</amazon>
</book>
 
<book>
<title>The Music of Chance</title>
<year>1990</year>
<publisher>Viking</publisher>
<amazon price="10.36">http://www.amazon.com/exec/obidos/ASIN/0140154078/ref=pd_bxgy_text_1/102-2599247-6584128</amazon>
</book>
 
<book>
<title>The New York Trilogy</title>
<year>1990</year>
<publisher>Penguin Books</publisher>
<amazon price="10.47">http://www.amazon.com/exec/obidos/ASIN/0140131558/qid=1021947332/sr=2-1/ref=sr_2_1/102-2599247-6584128</amazon>
</book>
</collection>
____________________________________________________

The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.

An XSL style sheet consists of one or more set of rules that are called templates. <xsl:template/>  template contains rules to apply when a specified node is matched.

 

<xsl:value-of> Element: The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation

                         

<xsl:for-each> element allows you to do looping in XSLT.

 

To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file

 

The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to do the multiple conditional tests.

 

Below is the  total.xsl used to convert:

 

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:template match="/">
<html>
   <head>
                  <title>Books by Paul Auster</title>
               <link rel="stylesheet" href="total.css" type="text/css"/>
   </head>
   <body>
 
               <div id="container">
 
               <xsl:for-each select="collection/book">
                               <xsl:sort select="title" /> 
                               <h2 >
                               Title: <span class="booktitle"><xsl:value-of select="title"/></span>
                               </h2>
 
                               <p>
                               <strong>Year of first publication::</strong> <xsl:value-of select="year/."/>
                               </p>
                               <p>
                                              <strong>Publisher: </strong>
                                              <xsl:value-of select="publisher/."/>
                               </p>
                               <p>
                                              <strong>Note:</strong> <xsl:value-of select="note/."/>
                               </p>
                               <p>
                                              <strong>Amazon URL:</strong> <a><xsl:attribute name="href">
                                              <xsl:value-of select="amazon/."/></xsl:attribute> 
                                              <xsl:value-of select="amazon/."/></a>
                               </p>
                               <p>
                                              <strong>Price at Amazon:</strong> 
                                              <xsl:choose>
                                                             <xsl:when test="amazon/@price >10.40"> 
                                                             <span title="That's expensive" class="expense">
                                                             <xsl:value-of select="amazon/@price" />
                                                             </span> 
                                                             </xsl:when>
                                                             <xsl:otherwise> 
                                                             <xsl:value-of select="amazon/@price" />
                                                             </xsl:otherwise>
                                              </xsl:choose>
                               </p>
                               </xsl:for-each>
                               <hr />
                               <div class="total"><strong>Total Cost of all listed books with prices:
                               <xsl:value-of select="sum(/collection/book/amazon/@price)" /></strong>  
                               </div>
                               </div>
</body>
 
</html>
</xsl:template>
</xsl:stylesheet>
 
 



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)