Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Transforming UTF8 XML files to text using XSLT? Encoding issues...

843834Jan 26 2009 — edited Mar 9 2010
Hi All

I am having a particularly thorny problem trying to transform a UTF8 encoded XML file into a UTF8 encoded text file using XSLT.

The following is an example of the source XML
[test.xml]
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type="text/xsl" href="my.xslt"?>
<root>
	<transcript>
		<item id="0"><![CDATA[Ich bin José Mourinho. Ich bin Fußballtrainer. Ich liebe dieses Spiel, und wenn Sie mir einen Fußballplatz, zwei Tore, ein paar Bälle und einige Spieler mit großen Träumen geben...]]></item>
		<item id="2"><![CDATA[Eines der Handicaps, die wir hier haben, ist, dass die Spieler, die besseren Spieler... sich im Wettbewerb nicht besonders reinhängen müssen.]]></item>
	</transcript>
</root>
Of course, we have source XML files in LOTS of different languages, including Asian languages such as Japanese and Korean, all correctly UTF8 encoded.

here is an example of my XSLT [my.xslt]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="text"/>
	
	<xsl:template match="/">
		<xsl:for-each select="root/transcript/item">
			For itemID == <xsl:value-of select="@id"/> the text is "<xsl:value-of select="."/>"
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
The code I have used to perform the XSLT is as follows;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class XSLTTestClass {

	public static void main(String[] args) {
		try {
			Source xmlSource = new StreamSource(new InputStreamReader(
					new FileInputStream("test.xml"), "UTF8"));
			Source xsltSource = new StreamSource(new InputStreamReader(
					new FileInputStream("my.xslt"), "UTF8"));
			Result result = new StreamResult(new OutputStreamWriter(
					new FileOutputStream("output.txt"), "UTF8"));

			// create an instance of TransformerFactory
			TransformerFactory transFact = javax.xml.transform.TransformerFactory
					.newInstance();

			Transformer trans = transFact.newTransformer(xsltSource);

			trans.transform(xmlSource, result);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
When I view the XML file in Firefox, the Firefox XSLT engine correctly shows the transformed text as UTF8 encoded. This was how I originally tested my XSLT to ensure it worked.

Using firefox the output is correctly formatted UTF8 as follows
	For itemID == 0 the text is "Ich bin José Mourinho. Ich bin Fußballtrainer. Ich liebe dieses Spiel, und wenn Sie mir einen Fußballplatz, zwei Tore, ein paar Bälle und einige Spieler mit großen Träumen geben..."
	For itemID == 2 the text is "Eines der Handicaps, die wir hier haben, ist, dass die Spieler, die besseren Spieler... sich im Wettbewerb nicht besonders reinhängen müssen."
However using the Java code the output.txt is as follows
	For itemID == 0 the text is "Ich bin José Mourinho. Ich bin Fußballtrainer. Ich liebe dieses Spiel, und wenn Sie mir einen Fußballplatz, zwei Tore, ein paar Bälle und einige Spieler mit großen Träumen geben..."
	For itemID == 2 the text is "Eines der Handicaps, die wir hier haben, ist, dass die Spieler, die besseren Spieler... sich im Wettbewerb nicht besonders reinhängen müssen."
Which contains weird escape characters for the special UTF8 characters.
And furthermore the output.txt file is also CP1252 encoded. Something I did not expect as both the reader and the writer are defined as UTF8.

Does anyone know why the file being generated, output.txt, is being written in CP1252 even though I am explicitly define a UTF8 encoded writer?
I tried the above stripping out the CDATA tags and the problem still occurs.

Thanks in advance,

Martin Graney
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 6 2010
Added on Jan 26 2009
7 comments
3,452 views