Home
TOC Index |
Choosing the Transformation Engine
This section provides the information you need to help you choose between the interpreting transformer (Xalan) and the compiling transformer (XSLTC).
Performance Considerations
For a single-pass translation, the interpreting transformer (Xalan) tends to be slightly faster than the compiling transformer (XSLTC), because it isn't generating and saving the byte-codes in the small Java classes that are run as translets.
But when a transformation will be used multiple times, it makes sense to use the XSLTC transformation engine because, in such settings, XSLTC is the clear winner when it comes to memory requirements and performance.
An XSLTC translet tends to be small, because it implements only those translations that the stylesheet actually performs. And it tends to be fast, both because it is smaller and because the lexical handling necessary to interpret the stylesheet has already been performed. Finally, translets tends to load faster and generally be more sparing of system resources, due to their small size.
For example, a servlet that will be running for long periods of time tends to benefit by using XSLTC. Similarly, a transformation that is run from the command line tends to run faster when XSLTC is used. You'll see more about that process in Transforming from the Command Line.
In addition to making it possible to cache translets, XSLTC provides a number of other options to help you maximize performance:
- Control of inlining
- By default, XSLTC "inlines" transformation code, which means that the code responsible for translating an element contains the transformation code for all possible subelements of that element.
- For small and medium-size stylesheets, that implementation produces the fastest possible code. However, complex stylesheets tend to produce translets that are extremely large.
- To solve that problem, XSLTC lets you disable inlining. To do that, you use the
-n
option when compiling XSLTC translets from the command line. When generating an XSLTC transformer using a JAXP factory class, you use the factory'ssetAttribute()
method to set the "disable-inlining" feature
with code like this:TransformerFactory tf = new TransformerFactory(); tf.setAttribute("disable-inlining", Boolean.TRUE);- Document-model caching
- When XSLTC operates on XML data, it creates it's own internal Document Object Model (something like the W3C DOM you've already seen, only simpler). Since the construction of the document model takes time, XSLTC provides a way to cache the model, to help speed up subsequent transformations.
- That feature can come in handy in a servlet that serves up XML documents, for example. If a transform converts them to HTML when they are accessed on the Web, then caching the in-memory representation of the document can have a potentially large impact on performance. Here is a sample of the code you would use:
final SAXParser parser = factory.newSAXParser(); final XMLReader reader = parser.getXMLReader(); XSLTCSource source = new XSLTCSource(); source.build(reader, xmlfile);- The
source
object can then be reused in multiple transformations, without having to re-read the file.- Caching of compiled stylesheets
- XSLTC also lets you save compiled versions of stylesheets, so you can use them to create multiple
Transformer
objects more rapidly. For example, that kind of capability can improve the startup time of a multi-threaded servlet. If the servlet generates a hundred threads to service input requests, it can compile the stylesheet once and then use the compiled version to generate a transformer for each thread.- Precompiled stylesheets are stored in
Templates
objects. When you create aTransformer
object directly (without using aTemplates
object), you use code like this:TransformerFactory factory = TransformerFactory.newInstance(); Transformer xformer = factory.newTransformer(myStyleSheet); xformer.transform(myXmlInput, new StreamResult(System.out));- But you can also create an intermediate Templates object that you can save and reuse, like this:
TransformerFactory factory = TransformerFactory.newInstance(); Templates templates = factory.newTemplates(myStyleSheet); Transformer xformer = templates.newTransformer(); xformer.transform(myXmlInput, new StreamResult(System.out));
Note: There are also rules for things to do and things to avoid when designing your stylesheets, in order to get maximum performance with XSLT. For more information on that subject, seehttp://xml.apache.org/xalan-j/xsltc/xsltc_performance.html
.
Functionality Considerations
While XSLTC tends to be a higher performance choice for many applications, Xalan has some advantages in functionality. Among those advantages are the support for the standard query language, SQL.
Making Your Choice
Whether you get the Xalan or XSLTC transformation engine is determined by factory configuration settings. By default, the JAXP factory creates a Xalan transformer. To get an XSLTC transformer, the preferred method is to set the
TransformationFactory
system property like this:javax.xml.transform.TransformerFactory= org.apache.xalan.xsltc.trax.TransformerFactoryImplAt times, though, it is not possible to set a system property -- for example, because the application is a servlet, and changing the system property would affect other servlets running in the same container. In that case, you can instantiate the XSLTC transformation engine directly, with a command like this:
new org.apache.xalan.xsltc.trax.TransformerFactoryImpl(..)You could also pass the factory value to the application, and use the ClassLoader to create an instance of it at runtime.
Note: To explicitly specify the Xalan transformer, you would use the valueorg.apache.xalan.processor.TransformerFactoryImpl
, instead oforg.apache.xalan.xsltc.trax.TransformerFactoryImpl
.
There is also a "smart transformer" that uses the Xalan transform engine when you generate
Transformer
objects, and the XSLTC transform engine when you generate intermediateTemplates
objects. To get an instance of the smart transformer, use the valueorg.apache.xalan.xsltc.trax.SmartTransformerImpl
either to set the transformer factory system property or use that class to instantiate a parser directly.
Home
TOC Index |
This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.
All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.