Extensible Stylesheet Language Transformations (XSLT) is an XML-based language that transforms an XML documents and generates output into a different format such as HTML, XML or another type of document that is recognized by a browser like WML, and XHTML.
XSLT is an extension of XSL, which is a stylesheet definition language for XML.
With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, and make decisions about which elements to hide and display.
XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.
The following figure shows the working process of XSLT:
The job of an XSLT processor is to apply an XSLT stylesheet to an XML source document and produce a result document, (for example HTML document). There are several XSLT processors, but a few good one (Open sources), such as MSXML4, Saxon, and Xalan, XT, Oracle. Most of them can be downloaded free from Web sites.
Apache's Xalan XSLT engine
Xalan is the Apache XML Project's XSLT engine. This processor is available at http://xml.apache.org/xalan/ . We will concentrate on using this engine for transformation our XML document that we have developed and want to transform it into output document in the HTML format.
Once the Xalan.zip or .gzip file is downloaded, unpack it and add these files to your CLASSPATH. These files include the .jar file for the Xerces parser, and the .jar file for the Xalan stylesheet engine itself. The .jar files are named xercesImpl.jar , and xalan.jar .
Working with XSLT APIs
XSLT consist of three components that transform an XML document into the required format. These components are:
TransformerFactory is an abstract class used to create an instance of the Transformer class that is responsible for transforming a source object to a result object.
The process of XML transformation starts when you create an instance of the TransformerFactory class. An instance of the Transformer class is then created using the instance of the TransformerFactory class. This instance of the Transformer class uses the XML document as a source object and optionally uses the predefined instructions required for transformation to generate the formatted output as a result object. You can create the source XML document using SAX, DOM, or an input stream. The result object of the transformation process is in the form of a SAX event handler, DOM, or an output stream.
During transformation process, the original document is not changed; rather, a new document is created based on the content of an existing one. The new document may be serialized (output) by the processor in standard XML syntax or in another format, such as HTML or plain text.
The following figure shows the working process of XSLT APIs:
The XSLT Packages
The XSLT APIs is defined in the following packages:
Defines the TransformerFactory and Transformer classes. These classes are used to get an object for doing transformations. After creating a transformer object, its transform() method is invoked. This method provides an input (source) and output (result).
Defines classes used to create input and output objects from a DOM.
Defines classes used to create input from a SAX parser and output objects from a SAX event handler.
Defines classes used to create input and output objects from an I/O stream.
In this tutorial, we will convert a simple XML file into HTML using XSLT APIs.
To develop this program, do the following steps:
1. Create an XML file
The code for the emp.xml file is given below:
<?xml version = "1.0" ?>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Nisha </Emp_Name>
<Emp_E-mail> Nisha1@yahoo.com </Emp_E-mail>
<Emp_Id> E-002 </Emp_Id>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>
2. Create an XSL Stylesheet
Lets see the source code of XSL stylesheet ( emp.xsl ) that provides templates to transform the XML document:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<title>XSLT Style Sheet</title>
<h1><p align="center">Employee Details</p></h1>
<table border="2" width="50%" align="center">
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial
Discuss: Introduction to XSLT
Post your Comment