all groups > dotnet xml > february 2004 >
You're in the

dotnet xml

group:

who controls the encoding when transforming xml?



who controls the encoding when transforming xml? Jiho Han
2/6/2004 4:08:08 PM
dotnet xml: I do a transform of an xml document into another xml using XslTransform.
In my xsl file, I specify using <xsl:output encoding="utf-8"/>. However,
when my transform is done, the resulting xml is in utf-16.
What gives? Anyone?

Re: who controls the encoding when transforming xml? Steven Livingstone
2/7/2004 12:33:12 AM
Your Xml doc is probably UTF-16 encoded. You might want to check what
encoding the Xml doc was saved with. Maybe even try saving it as UTF-8 - in
notepad say.

Any joy?

Steven
Founder, http://venturetogether.com

[quoted text, click to view]

Re: who controls the encoding when transforming xml? Oleg Tkachenko [MVP]
2/8/2004 12:34:07 PM
[quoted text, click to view]

Show us your code. <xsl:output encoding="utf-8"/> only works when you
transform to Stream. Otherwise it's ignored and other stuff defines
output encoding.
--
Oleg Tkachenko [XML MVP, XmlInsider]
Re: who controls the encoding when transforming xml? Jiho Han
2/9/2004 2:00:24 PM
This is my xsl header section:

<?xml version="1.0" ?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output encoding="utf-8" indent="no" media-type="text/xml"
method="xml"/>

This is my data:

<association>

<associationid>5b5acf84-6a76-4447-ad88-dc0b7d031741</associationid>



</association>

Here's my code for the transform:

XmlDocument xdoc = new XmlDocument();

xdoc.LoadXml(dataXml);

XmlDeclaration xmldecl;

xmldecl = xdoc.CreateXmlDeclaration("1.0", null, null);

xmldecl.Encoding="UTF-8";

xdoc.InsertBefore(xmldecl, xdoc.DocumentElement);

XslTransform xslt = new XslTransform();

xslt.Load(Server.MapPath("client/association.xsl"));

StringBuilder sb = new StringBuilder();

StringWriter sw = new StringWriter(sb);

xslt.Transform(xdoc, null, sw);

return sb.ToString();

As you can see I tried to insert a XML declaration in there but it really
didn't do much.
Thanks for looking into this. Much appreciated.
Jiho

[quoted text, click to view]

Re: who controls the encoding when transforming xml? Steven Livingstone
2/9/2004 8:13:56 PM
Instead of using StringWriter, try using a Stream such as follows :

MemoryStream ms = new MemoryStream();
xslt.Transform(xdoc, null, ms);

ms.Position = 0;
StreamReader sr = new StreamReader(ms, Encoding.UTF8);

return(sr.ReadToEnd());

This wiill output a UTF-8 encoded string.

Steven, XmlInsider
http://stevenlivingstone.com


[quoted text, click to view]

Re: who controls the encoding when transforming xml? Oleg Tkachenko [MVP]
2/9/2004 10:03:37 PM
[quoted text, click to view]

Encoding of source XML document actually has noting to do with output
encoding.

[quoted text, click to view]

As stated here -
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinputsoutputstoxsltransform.asp?frame=true
when transforming to TextWriter (StringWriter is TextWriter), encoding
attribute of xsl:encoding element is ignored and instead encoding of the
TExtWriter is used. Strings are always UTF-16 encoded hence with
StringWriter the result is alwaus UTF-16 encoded.
If you want to control output encoding from within XSLT stylesheet,
transform to Stream, such as MemoryStream.
--
Oleg Tkachenko [XML MVP, XmlInsider]
AddThis Social Bookmark Button