MyCrystal


I use xslt document to transform xml. The xslt doc's output setting is as follows:

<xslTongue Tiedtylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="www.stl.com">
<xslSurpriseutput method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

At runtime, XslCompiledTransform's OmitXmlDeclaration is true and the Encoding is UTF-8, however, the transformed xml output has xml declaration and the encoding is UTF-16.

< xml version="1.0" encoding="UTF-16" >

i really appreciate if someone can explain why the output xml is different from the OmitXmlDeclaration's runtime value.



Re: OmitXmlDeclaration of XslCompiledTransform

Derek Smyth


Hi,

It might be the writer which is writing the transformed XML to file that is including the declaration. For example if your using the XmlWriter, rather than a FileStream, to write the transformed XML then it might be writing the declaration. The writer could have the UTF-16 encoding set.






Re: OmitXmlDeclaration of XslCompiledTransform

MyCrystal

Thank you for you help. Derek, you are right. I use XmlWriter with the Transform method and i didn't set XmlWriterSettings's OmitXmlDeclaration to true(defalut value is false). The following code works:

XslCompiledTransform xslt1 = new XslCompiledTransform();
xslt1.Load(reader1);

XPathDocument doc1 = new XPathDocument(new StringReader(source));

StringBuilder sb1 = new StringBuilder();

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Encoding = System.Text.Encoding.UTF8;
XmlWriter xsltOutput1 = XmlWriter.Create(sb1,settings);

But i still has a question. when the XmlWriter is being used with the Transform method, does the XslCompiledTransform's OutputSettings such as OmitXmlDeclaration & Encoding has no effect in the xml declaration and encoding of the transformed xml






Re: OmitXmlDeclaration of XslCompiledTransform

Derek Smyth

Hi,

I'm glad that worked out for you. This is the first time I've seen this problem but I've generally kept the xml declaration in the XML files; I believe the declaration contains important information about the file.

The xml that your transformation creates should follow the stylesheet output options. If you just wrote the XML to file using a FileStream in the transformation then I don't think you'd have had the declaration. Since the XmlWriter was used it placed it's own declaration in, it would be interesting to see what would happen if the transformed xml had a declaration, would the XmlWriter write another one

If you add a break point and watch on your StringBuilder you should be able to check what XML is being produced by your stylesheet.

The other thing is with both encoding and omit-declaration settings used there could be a conflict. The declaration contains the encoding so perhaps the encoding setting takes precedence over the other. So regardless of the omit-declaration setting maybe if encoding is set a declaration is automatically written. Unfortunately I don't know if this is true but the way to find out would be to check the string held in your StringBuilder after the transformation.

Hopefully that was helpful.






Re: OmitXmlDeclaration of XslCompiledTransform

Martin Honnen

If you are transforming to an XmlWriter and want the xslSurpriseutput settings of the stylesheet to be used then you are supposed to pass the property OutputSettings of the XslCompiledTransform instance to XmlWriter.Create e.g.

Code Snippet

using (XmlWriter xmlWriter = XmlWriter.Create(sb1, xslt1.OutputSettings)) {

xslt1.Transform(doc1, null, xmlWriter);

}






Re: OmitXmlDeclaration of XslCompiledTransform

Derek Smyth

I did not know that! Thanks Martin.




Re: OmitXmlDeclaration of XslCompiledTransform