views:

38

answers:

2

I'm currently working on a Java web project (Spring) which involves heavy use of xsl transformations. The stylesheets seldom change, so they are currently cached. I was thinking of improving performance by compiling the xsl-s into class files so they wouldn't have to be interpreted on each request.

I'm new to Java, so I don't really know the ecosystem that well. What's the best way of doing this (libraries, methods etc.)?

Thanks,
Alex

+1  A: 

You'll need a Template and a stylesheet cache:

http://onjava.com/pub/a/onjava/excerpt/java_xslt_ch5/index.html?page=9

Do be careful about thread safety, because Transformers aren't thread safe. Best to do your transformations in a ThreadLocal for isolation.

duffymo
+2  A: 

You might not have to compile to .class to achieve your goal, you can compile the xsl once per run and re-use the compiled instance for all transformations.

To do so you create a Templates object, like:

TransformerFactory factory = TransformerFactory.newInstance();
factory.setErrorListener(new ErrorListener( ... ));
xslTemplate = factory.newTemplates(new StreamSource( ... ));

and use the template to get a transformer to do the work:

Transformer transformer = xslTemplate.newTransformer();

Depending on the XSL library you use your mileage may vary.

rsp