Monday, May 11, 2009

XSLT For Novice

What is XSLT?
  • XSLT stands for XSL Transformations
  • XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.
  • XSLT is the most important part of XSL
  • XSLT transforms an XML document into another XML document
  • XSLT uses XPath to navigate in XML documents
  • XSLT is a W3C Recommendation
In this tutorial, we want to transform the following XML document ("Books.xml") into HTML document (“Books.html”) using XSLT document (“Books.xsl”)

<?xml version="1.0" encoding="ISO-8859-1"?>
<book ID = "888">
<author>John Smith</author>
<title>New Cars</title>
<book ID = "999">
<author>Dan Big</author>
<title>Large Stories</title>

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
<xsl:template match="/">
<head><title>Book List</title></head>
<table border="1" cols="3" width="100%">
<xsl:apply-templates />
<xsl:template match="book">
<td><xsl:value-of select="@ID" /></td>
<xsl:apply-templates select="title|price" />
<xsl:template match="title|price">
<td><xsl:value-of select="." /></td>

XSLT <xsl:stylesheet> Element
  • The <xsl:stylesheet> defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes).
XSLT <xsl:template> Element
  • The <xsl:template> element is used to build templates.
  • An XSL style sheet consists of one or more set of rules that are called templates.
  • A template contains rules to apply when a specified node is matched.
  • The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).
XSLT <xsl:apply-templates> Element
  • The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.
  • If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.
XSLT <xsl:value-of> Element
  • The <xsl:value-of> element is used to extract the value of a selected node
The result is :
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Book List</title>
<table width="100%" cols="3" border="1">
<td>888</td><td>New Cars</td><td>$8.00</td>
<td>999</td><td>Large Stories</td><td>$9.00</td>

How to test using command line utility?
1. Download Xalan-Java.
2. Set the Java classpath to include xalan.jar, serializer.jar,xml-apis.jar, and xercesImpl.jar -- or another conformant XML Parser
3. Call java and the Process class with the appropriate flags and arguments (described below). The following command line, for example, includes the -IN, -XSL, and -OUT flags with their accompanying arguments -- the XML source document, the XSL stylesheet, and the output file:
java org.apache.xalan.xslt.Process -IN Books.xml -XSL Books.xsl -OUT Books.html
Create batch file
java -classpath .;serializer-2.7.1.jar;xalan-2.7.1.jar;xercesImpl.jar;xml-apis.jar org.apache.xalan.xslt.Process -IN Books.xml -XSL Books.xsl -OUT Books.html


1 comment: