Hi everyone,
I have a simple XML with one UTF-8 character É like this:
<?xml version="1.0" encoding="UTF-8"?>
<x10> <x69 x67='Tax_ID' x68='É'/> </x10>
The transform is an identity transform:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
But after transform, the output changed from
Before tansform: <?xml version="1.0" encoding="UTF-8"?><x10> <x69 x67="Tax_ID" x68="É"/> </x10>
to
After transform: <?xml version="1.0" encoding="UTF-8"?><x10> <x69 x67="Tax_ID" x68="Ã"/> </x10>
The character is not correctly encoded.
Here is my testing code, the xml lib is xalan:
public class TransformTest
{
private static String ENC = "UTF-8";
private static String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<x10> <x69 x67=\"Tax_ID\" x68=\"É\"/> </x10>";
private static String 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=\"xml\" indent=\"yes\" encoding=\"UTF-8\"/>" +
"<xsl:template match=\"@*|node()\">" +
"<xsl:copy>" +
"<xsl:apply-templates select=\"@*|node()\"/>" +
"</xsl:copy>" +
"</xsl:template>" +
"</xsl:stylesheet>";
private static TransformerFactory factory = TransformerFactory.newInstance();
private static StreamSource xslSource = null;
private static StreamSource xmlSource = null;
public TransformTest()
{
}
/**
* @param args
*/
public static void main( String[] args )
{
System.out.println( "Before tansform: " + xml );
try
{
xslSource = new StreamSource( new InputStreamReader( new ByteArrayInputStream( xslt.getBytes(ENC)), ENC ));
xmlSource = new StreamSource( new InputStreamReader( new ByteArrayInputStream( xml.getBytes(ENC)), ENC ) );
Templates templates = factory.newTemplates( xslSource );
Transformer transformer = templates.newTransformer();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
StreamResult result = new StreamResult ( new OutputStreamWriter (bao, ENC));
transformer.transform( xmlSource, result );
System.out.println("After transform: " + bao.toString());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Any helps are appreciated!
Thanks,
Mian