UTF-8 bug in JSTL's xml transform tag 
While going through the process of making
work's current project UTF-8 compliant I encountered a nasty bug/problem.
I successfully changed all the java files to UTF-8 without any headache. When I got to the JSPs there were a number of little tricks, being:
- using both contentType and pageEncoding attributes in the page directive, like
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
- putting in a filter so ensure a call to
request.setCharacterEncoding("UTF-8"); before any jsp or struts form processing
- adding the attribute charEncoding="UTF-8" to all c:import tags, like
<c:import url="http://www.wever.org" charEncoding="UTF-8"/>
- changing the xsls in the database to UTF-8 encoding declaration and UTF-8 output attribute.
But the bug/problem was using XSLT TRaX from inside the JSPs.
For example the jsp code:
<c:import var="xslt" url="servlet/Xsl?id=123" charEncoding="UTF-8"/>
<c:import var="xml" url="servlet/Xml?id=321" charEncoding="UTF-8"/>
<x:transform xml="${xml}" xslt="${xslt}"/>
was not working, even when I could get exactly the same xml and xsl to transform correctly with the same Transformer in plain java code.
The result was just junk. Not so much wierd characters and entities, but the xml was getting truncated during transformation and simply outputting the same text for every xsl template in the xsl.
Wierd and I was slowly going nuts trying to fix it! Lots of googling and xslt trax source code reading got me nowhere. I even tried using JSLT 2.0, it doesn't work with Oracle 10g yet. (I believe it's working in the 10g 10.0.3 preview).
We were using Sun's JDK1.5 XSLT TRaX default tranformer:
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
and the last thing on my list to try before hitting the sack at 1am sunday morning was to switch transformers:
- (Oracle) I've never been able to get Oracle's oracle.xml.jaxp.JXSAXTransformerFactory to work with jstl's xml tags. (Oracle 10g doesn't officially support jdk1.5, and they have no announcement of a timeline to supporting jdk1.5 yet.)
- (Xalan) I tried org.apache.xalan.processor.TransformerFactoryImpl but this gave the same result as jdk1.5's default not surprisingly (it's basically the same implemenation).
- (Jd.xslt) I could not download jd.xslt as the website was down,
- (Saxon) so last I tried Saxon8's net.sf.saxon.TransformerFactoryImpl (saved this to last because of it's download size, and bloat of additional features that I didn't really need to add to the web application).
Viola! Saxon worked! Happy Days!If someone knows what the real problem is, or if it's been fixed in the other transformer implemenatations please
email me. Otherwise I think I'm now a Saxon fan.