Getting started with XSLTC
I often hear two major complaints about XSLT. The first is about its syntax, which takes a while to get used to. Coupled with its recursive nature, the chronological mind can have a hard time adjusting. In fairness, with XSLT editing tools and a bit of practice, the syntax problem can be resolved. The second complaint is on performance. A while ago I was involved in a portal project. The different boxes that make up the portal were each painted via an XSL transformation, as was the entire portal page. From the prototyping stage it became evident we had a performance problem at hand. We have been taking a variety of approaches to the problem including caching, using Java threads to distribute the transformations for each box, and experimenting with various XML and XSL parsers.
Most recently, we have been working with an XSLT compiler from Sun. Preview version 5 was released in March 2001. This tool turns an XSLT stylesheet into a Java class file called a translet. The translet can then be used in conjunction with an XML file to apply the transformations. Although the software is not in final release form, our initial results have been encouraging. Since the entire package is written in Java, it is quite portable across platforms. In fact, one of the examples is a translet converted to the Palm format to be executed within the KVM.
After you unpack the XSLTC download, move the BCEL jar file to the lib directory and add the bin directory to your path. You should also define a new environment variable called XSLT that is set to the installation directory of XSLTC. Now take your favorite XSLT file and enter the following:
This should invoke either the xsltc.bat file (Windows) or the xsltc.sh (Unix). It will attempt to execute the class com.sun.xslt.compiler.XSLTC with the XSL file as its argument. After completion, you should see a file called foo.class in the same directory. You can compile directly into a jar file using the j option. This is the Translet file. You can now use the xslt command (also a batch file or a shell script) to apply the compiled XSLT file to an XML file as follows:
xslt foo.xml foo
This will cause the com.sun.xslt.runtime.DefaultRun class to be invoked, which takes an XML file and a Translet as arguments. While the XSLT compiler requires Java 2, to run the translet, you can use J2ME. Thats what the Palm sample application demonstrates.
Ideally you would want to use translets inside your Java application. All translets implement the interface com.sun.xslt.translet so you should start there. Youll see that the XML file is parsed into a DOM tree and then the transformation (specified by the translet) is applied to it (via the transform() method). The output is similar to a SAX event stream based on an implementation of com.sun.xslt.TransletOutputHandler.
XSLTC supports most of the XSLT specification. Some of the features that are not supported are namespace axis, pattern matching with id and key, and the ability to call nonstatic external Java methods. For projects struggling with XSLT performance, XSLTC may prove to be a viable option.
- Piroz Mohseni
Did you know...
The W3C has moved XML Schema to a Proposed Recommendation status via an announcement on March 16. The move is further indication of the stability of the current specification. All three parts of the specification are expected to be finalized paving the way for the long-awaited data-type validation for XML documents.