Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

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.

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.

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

<Employee-Detail>

<Employee>

<Emp_Id> E-001 </Emp_Id>

<Emp_Name> Nisha </Emp_Name>

<Emp_E-mail> Nisha1@yahoo.com </Emp_E-mail>

</Employee>

<Employee>

<Emp_Id> E-002 </Emp_Id>

<Emp_Name> Amit</Emp_Name>

<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>

</Employee>

<Employee>

<Emp_Id> E-003 </Emp_Id>

<Emp_Name> Deepak </Emp_Name>

<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>

</Employee>

</Employee-Detail>

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"/>

<xsl:template match="/">

<html>

<title>XSLT Style Sheet</title>

<body>

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

<xsl:apply-templates/>

</body>

</html>

</xsl:template>

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

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

<tr bgcolor="LIGHTBLUE">

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

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

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

</tr>

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

<tr>

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

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

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

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.