XSLTC
Overview
XSLTC is the compiler version of XSLT transforms.
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.
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's setAttribute() 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 a Transformer object directly (without using a Templates 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, see http://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 value org.apache.xalan.processor.TransformerFactoryImpl, instead of org.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 intermediate Templates objects. To get an instance of the smart transformer, use the value org.apache.xalan.xsltc.trax.SmartTransformerImpl either to set the transformer factory system property or use that class to instantiate a parser directly.