Search code examples
javaentityescapingtransformer-modelxslt

How Do You Prevent A javax Transformer From Escaping Whitespace?


I'm using the javax.xml.transform.Transformer class to perform some XSLT translations, like so:

TransformerFactory factory = TransformerFactory.newInstance();
StreamSource source = new StreamSource(TRANSFORMER_PATH);
Transformer transformer = factory.newTransformer(source);
StringWriter extractionWriter = new StringWriter();
String xml = FileUtils.readFileToString(new File(sampleXmlPath));
transformer.transform(new StreamSource(new StringReader(xml)),
        new StreamResult(extractionWriter));
System.err.println(extractionWriter.toString());

However, no matter what I do I can't seem to avoid having the transformer convert any tabs that were in the source document in to their character entity equivalent (	). I have tried both:

transformer.setParameter("encoding", "UTF-8");

and:

transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

but neither of those help. Does anyone have any suggestions? Because:

&#9;&#9;&#9;&#9;&#9;<MyElement>

looks really stupid (even if it does work).


Solution

  • So the answer to this one turned out to be pretty lame: update Xalan. I don't know what was wrong with my old version, but when I switched to the latest version at: http://xml.apache.org/xalan-j/downloads.html suddenly the entity-escaping of tabs just went away. Thanks everyone for all your help though.