Xsl Transform Encoding UTF-8

Hi all,

I´m using the xsl_transform action in a XML to clear all the Tags that are empty. This action allways encode the XML with UTF-16 instead of UTF-8 that i need. (<?xml version="1.0" encoding="utf-16"?>)

I explicitly put the encoding to UTF-8 but with no sucess.

Can anyone help?

This is the xsl that i'm using:

"<xsl:stylesheet version='1.0'
 <xsl:output version='1.0' encoding='UTF-8' indent='yes'/>
 <xsl:strip-space elements='*'/>

 <xsl:template match='node()|@*'>
   <xsl:apply-templates select='node()|@*'/>

 <xsl:template match='*[not(@*) and not(*) and (not(text()) or .=-1)]'/>
Hi there,
I took a look at the code of XSL_Transform (.NET Stack) and I believe you won't be able to do it, it uses the XslTransform object writing to a StringWriter object which will always write in UTF-16.

You could write your own action using the XslCompiledTransform object, writing to a XmlWriter, where you can set the encoding you want (or extract from the stylesheet).

Otherwise, you can also try changing the encoding with the actions TextToBinaryData with encoding UTF-16, then use ConvertEncoding to convert to UTF-8, and then BinaryDataToText with encoding UTF-8. All of these actions are in the BinaryData extension.

Let me know if this helps.
Best regards,
Pedro Vieira

Hi Pedro,

thx for your reply and help!

I've created an extension with the same code of XSL_Transform (.NET Stack) but with the add of the following subclass:

public class Utf8StringWriter : StringWriter
        public override Encoding Encoding
            get { return Encoding.UTF8; }

then i used the Utf8StringWriter where the StringWriter is used and it works ;)

Best regards,
Nuno André
Yet another fine solution! ;-)