Introduction to XSLT

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.

Introduction to XSLT

Introduction to XSLT

     

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:

XSLT Processors

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:

  • An instance of the TransformerFactory
  • An instance of the Transformer
  • The predefined transformation instruction

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:

Package

Description

javax.xml.transform

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

javax.xml.transform.dom

Defines classes used to create input and output objects from a DOM.

javax.xml.transform.sax

Defines classes used to create input from a SAX parser and output objects from a SAX event handler. 0

javax.xml.transform.stream

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. 1

To develop this program, do the following steps:

1. Create an XML file

The code for the emp.xml file is given below: 2

<?xml version = "1.0" ?>

<Employee-Detail>

<Employee> 3

<Emp_Id> E-001 </Emp_Id>

<Emp_Name> Nisha </Emp_Name>

<Emp_E-mail> [email protected] </Emp_E-mail> 4

</Employee>

<Employee>

<Emp_Id> E-002 </Emp_Id> 5

<Emp_Name> Amit</Emp_Name>

<Emp_E-mail> [email protected] </Emp_E-mail>

</Employee> 6

<Employee>

<Emp_Id> E-003 </Emp_Id>

<Emp_Name> Deepak </Emp_Name> 7

<Emp_E-mail> [email protected] </Emp_E-mail>

</Employee>

</Employee-Detail> 8

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" ?> 9

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html" indent="yes"/>

<xsl:template match="/"> 0

<html>

<title>XSLT Style Sheet</title>

<body> 1

<h1><p align="center">Employee Details</p></h1>

<xsl:apply-templates/>

</body> 2

</html>

</xsl:template>

<xsl:template match="Employee-Detail"> 3

<table border="2" width="50%" align="center">

<tr bgcolor="LIGHTBLUE">

<td><b>Emp_Id</b></td> 4

<td><b>Emp_Name</b></td>

<td><b>Emp_E-mail</b></td>

</tr> 5

<xsl:for-each select="Employee">

<tr>

<td><i><xsl:value-of select="Emp_Id"/></i></td> 6

<td><xsl:value-of select="Emp_Name"/></td>

<td><xsl:value-of select="Emp_E-mail"/></td>

</tr> 7

</xsl:for-each>

</table>

</xsl:template> 8

</xsl:stylesheet>