When moving to from XslTransform to XslCompiledTransform...

by Michael S. Kaplan, published on 2007/04/30 03:01 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2007/04/30/2333104.aspx


Marco asked:

Given a XSLT stylesheet with the following element:

        <xsl:output method="html" encoding="Windows-1252"/>

NET Framework 1.1 System.Xml.Xsl.XslTransform.Transform method outputs:

        <META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">

Using NET Framework 2.0 System.Xml.Xsl.XslCompiledTransform.Transform method outputs:

        <META http-equiv="Content-Type" content="text/html; charset=utf-8">

I need the 1.1 output but using NET 2.0,

How can I do that?

Thank you very much in advance for any help,

Luckily Anton was ready with the answer:

If you pass a TextWriter/XmlWriter to the Transform() method, the encoding specified in the xsl:output element is ignored, and the encoding of that TextWriter/XmlWriter is used instead.  To respect the xsl:output encoding setting, you need to pass to the Transform() method either a Stream or a TextWriter/XmlWriter created with the desired encoding.  For example,

    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load("MyStylesheet.xsl");
    xslt.Transform("MyDocument.xml", XmlWriter.Create(myStream, xslt.OutputSettings));

This seems quite sensible, though the fact that this information does not seem to be captured in the migration information under either XslCompiledTransform.Transform or XslTransform.Transform methods (e.g. it is not in the Migrating From the XslTransform Class topic).

Which is why I thought I'd mention it here.... :-)

 

This post brought to you by (U+0e5b, a.k.a. THAI CHARACTER KHOMUT)


Joey Baghodoughnuts on 23 Dec 2008 11:32 AM:

Thanks for this posting.  It has made working with the silly .NET XSLCompiledTransform a lot easier.

-Joe


Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

go to newer or older post, or back to index or month or day