Home | Login | Blog | Forum | Download | RSS Feed | Ajax | Hello-World | J2EE | BPM | EJB | FileNet | Hibernate | ORM | IBM | ILOG | Interview | FAQs |

Java | Apache | JVM | Others | BRMS | Oracle | Pega | SOA | Sun-Certification | Web-Service | websphere | XML |
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

XML to HTML, XML to HTML using XSLT, XSLT Tutorial, XML Tutorial


Share
 
 

Useful Java Links


       


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>
 
 



Download Latest Java/J2EE eBooks, SCJP Dumps, SCWCD Dumps, Pega Tutorial, IBM WebSphere BPM tutorials and many more


If you need any urgent assistance on convert-xml-to-html-using-xslt, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

You can also subscribe to our newsletters on convert-xml-to-html-using-xslt, to receive updates on convert-xml-to-html-using-xslt,via email.Enter you email below:

Enter your email address:

Delivered by FeedBurner



Keyword Tags: convert-xml-to-html-using-xslt tutorial,convert-xml-to-html-using-xslt in java,Concepts of convert-xml-to-html-using-xslt,Java,J2EE,Interview Questions,convert-xml-to-html-using-xslt Examples


Comments:


Post Your Comment:

*

Yes, I would like to recieve email notifications on my reply.

Popular Posts:

 
Popular Downloads

SCJP 1.6 Dumps
JSP Interview Questions
Struts Interview Questions
Hibernate Interview Questions
SCWCD Dumps
SCBCD Dumps

Java/J2EE Tutorial
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial
Oracle 9i & 10g
MYSQL Tutorial
php Tutorial
JQuery Examples
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
All Tutorials
php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate
Java Collections
MAP
SET
LIST
VECTOR
Advanced Java
Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns
HOME | FORUM | ABOUT | BLOGS | CONTACT-US