Compiling .xsl files into .class files
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开发者_C百科 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,
AlexYou 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.
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.
Consider using Gregor/XSLT compiler
精彩评论