October 25, 2016
Hot Topics:

Optimizing Stylesheet Execution with the Java Transformation API for XML (TrAX)

  • August 27, 2002
  • By Jeff Ryan
  • Send Email »
  • More Articles »

We are still using a Transformer object to do the transformation. However, we get the transformer by asking the Templates object for a new Transformer object via newTransformer(). Tranformations are performed by passing a Source and Result to the transform() method.

This example is quite an improvement over SimpleTransform. By using the Templates object, we will now only compile a given stylesheet the first time it is used. However, this comes at a cost. As long as XslTransformer stays loaded in memory, we'll never get a new version of the stylesheet. In a high availability situation, this might not be acceptable because you would have to bounce the application server or JVM to deploy a new version of the stylesheet.

Hot Deployed Stylesheets

Java Server Pages (JSP) technology handles hot deployments very nicely. Whenever the source code for a .jsp changes, a "stale" check detects the change and automatically recompiles the JSP.

With a little bit of work, we should be able to use the stale checking approach to hot deploy file-based stylesheets as well. Whenever we construct a XslTransformer object for a stylesheet in the cache, we'll check to see if the .xsl file has changed. If it has changed, we'll recompile it and cache a new Templates object. Note that to add the hot deploy capability, we'll use features of the Java language, not of the TrAX API.

First we'll build our own TemplateWrapper object to wrap the TrAX Templates object and provide stale checking. The important part of this inner class is the isStale() method that determines whether the stylesheet source has changed since it was last compiled.

    class TemplateWrapper
        Templates stylesheet; // the compiled stylesheet
        File xslFile;         // represents the stylesheet source
        long timestamp;       // last compile time

        TemplateWrapper(Templates aStylesheet, String xslFileName)
            stylesheet = aStylesheet;
            xslFile    = new File(xslFileName);
            timestamp  = xslFile.lastModified();

        private boolean isStale()
            return xslFile.lastModified() != timestamp;
        Templates getStylesheet()
            return stylesheet;

Now we need to make some changes to the constructor of XslTransformer to perform the stale check, and recompile the stylesheet when necessary.

    public XslTransformer(String theXslFileName)
        throws TransformerConfigurationException
        xslFileName  = theXslFileName;
        TemplateWrapper stylesheet = (TemplateWrapper)CACHE.get
        if (stylesheet == null || stylesheet.isStale())
            TransformerFactory factory =

            Templates template = factory.newTemplates
                                 (new StreamSource(xslFileName));

            stylesheet = new TemplateWrapper
                             (template, xslFileName);
            CACHE.put(theXslFileName, stylesheet);
        transformer  = stylesheet.getStylesheet().newTransformer();

Now the cache is storing TemplateWrapper objects instead of Templates objects. The isStale() method is used to determine when we need to refresh the cache with a new TemplateWrapper. Now we have both the performance boost of compiled stylesheets and the hot deploy capability for high availability.


The Java Transformation API for XML (TrAX) is relatively easy to use. Learning how to compile stylesheets is one of the advanced features worth mastering because it will result in a big performance boost to your application. The ability to hot deploy stylesheets could be lost with the performance boost. By adding some simple Java to the mix, we can get both the optimizations and the hot deploy capability. The XslTransformer object we created demonstrates this feature. It could be the start of a utility transformation class or even a transformation service used in a real production application to encapsulate the TrAX API, but that part is up to you.

Code Examples

To download the example java files, click here.

Additional Resources

Information on the Java Transformation API for XML can be found at http://xml.apache.org/xalan-j/trax.html.

Other Articles Written by Jeff Ryan

About the Author

Jeff Ryan is an architect for Hartford Financial Services. He has eighteen years of experience designing and developing automated solutions to business problems.
His current focus is on Java, XML, and Web Services technology. He may be reached at jryan@thehartford.com.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel