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”)

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

Books.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title>Book List</title></head>
<body>
<table border="1" cols="3" width="100%">
<xsl:apply-templates />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="book">
<tr>
<td><xsl:value-of select="@ID" /></td>
<xsl:apply-templates select="title|price" />
</tr>
</xsl:template>
<xsl:template match="title|price">
<td><xsl:value-of select="." /></td>
</xsl:template>
</xsl:stylesheet>

Explanation
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 :
Books.html
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Book List</title>
</head>
<body>
<table width="100%" cols="3" border="1">
<tr>
<td>888</td><td>New Cars</td><td>$8.00</td>
</tr>
<tr>
<td>999</td><td>Large Stories</td><td>$9.00</td>
</tr>
</table>
</body>
</html>

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
OR
Create batch file
Books.cmd
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
pause

Resource
http://www.w3schools.com/xsl/
http://xml.apache.org/xalan-j/commandline.html

1 comment: