SL (eXtensible Stylesheet Language) enables you to transform an XML document into HTML, another XML document, or any plain text. Using XML with XSL separates the business logic data from the presentation data and allows application developers and Web designers to work independently with ease. It also simplifies application maintenance, as you can make changes to the presentation data without touching the business logic part of the application.
As simple as it sounds, doing transformation with XSL does have a drawback when done within the application: it can take a considerable amount of time and reduce performance. The time needed to parse XML and XSL documents is directly proportional to the size of the documents. Each transformation requires the XML and XSL documents to be loaded, syntax checked, and parsed. Developers can optimize all other bottlenecks in the application, but they cannot do anything to optimize XSL transformation. In the worst case, if the XSL file is loaded from a URL, it further degrades the performance of the application.
Translets, the latest addition to the Java application development framework, offer the perfect solution. Translets are precompiled XSL documents that are optimized and converted into simple Java classes. When you compile your application Java files, you compile your XSL files into Java class files. During runtime, you can load translets like any regular Java class and perform XSL transformations over and over again. The syntax checking and parsing of XSL documents are done when the XSL files are compiled. The transformation therefore takes only as long as the compiled code takes to execute, which improves performance multiple folds.
Since translets are simple Java classes, you can very easily jar and deploy them along with the application. Depending on the design of your Web applications, you can deploy and use the translets jar on either the client side or the server side.
This article walks you through the creation of translets, discusses various options that translet functionality provides, and finally demonstrates how to use them from within a Java application. If you already use XSL in your Web applications, you can readily apply the same concept to them.