XSL Output Encoding
843834Sep 12 2003 — edited Sep 19 2003I'm trying to do XSL transformation; my implementation works otherwise correctly, except that scandic letters like "�" and "�" get replaced with garbage like "��" (Note that the forum is encoding the symbols as '?'). The source document has no encoding attribute specified, and I have tried using "ISO-8859-1" and Windows "CP1252" as the xsl:output tag's encoding attribute. I have also tried setting the attribute via Transformer's setOutputProperty() method (I'm using the javax.xml.* classes and JDK XSL implementation, which is apparently Xalan), but that is no different from xsl:output.
Here's my code, although I suspect nobody actually needs it. Note that eventhough I am using strings as input and output, the same problem is also present in another version that uses the file streams directly so that's probably not contributing to the problem.
public String transform(String xml, String xsl) throws Exception {
Reader xmlReader = new StringReader(xml);
Reader xslReader = new StringReader(xsl);
StringWriter xmlWriter = new StringWriter();
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xslSource = new StreamSource(xslReader);
Templates stylesheet = factory.newTemplates(xslSource);
Transformer transformer = stylesheet.newTransformer();
StreamSource source = new StreamSource(xmlReader);
StreamResult result = new StreamResult(xmlWriter);
insertXSLParameters(transformer);
transformer.transform(source, result);
return xmlWriter.toString();
}
I have confirmed that when the xml-parameter enters the method, it has the valid data, and that the data is corrupted before it gets returned from the method. I have also basically tried all encoding combinations for ISO-8859-1, CP1252 and UTF-8, with the attribute specified either only in xsl:output or also in the source file's xml header, to no use. I have searched the forum but nobody seems to have provided an answer to this question - it surprises me that it has rarely even come up. Basically, the question, how to disable encoding and just use platform default thorought the whole process.