Florjon
Florjon

Reputation: 3629

xmltextwriter transforms special characters

I get special characters transformed in the result of an xslt file transformation.
Has anyone experienced this before?

In the source document there's a character & which in the result is presented as &. I need the the original & character even in the result.

XmlDataDocument dd = new XmlDataDocument(ds);

XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;
settings.EnableScript = true;

XslCompiledTransform transform = new XslCompiledTransform();

transform.Load(XmlReader.Create(new StringReader(transformSource.Transform)), settings, new XmlUrlResolver());

XsltArgumentList a = new XsltArgumentList();

a.AddExtensionObject("http://www.4plusmedia.tv", new TransformationHelper());

using (XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.UTF8))
{
    writer.Formatting = Formatting.Indented;
    transform.Transform(dd, a, writer);
}

Upvotes: 1

Views: 1575

Answers (3)

Martin Honnen
Martin Honnen

Reputation: 167571

If you want XslCompiledTransform to output a plain text file as a result of an XSLT transformation you should not transform to an XmlTextWriter you create, instead transform to a FileStream or TextWriter.

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273264

Your output part is using (XmlTextWriter writer = ...) { ... }

This indicates that your output is XML. You could use XSLT to produce plain text, that would be different.

For XML and HTML output the & encoding is necessary and essential.

At some stage the Value of your Xml elements will be used and that is where (and when) & becomes a & again.

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

In the source document there's a character & which in the result is presented as &.

Don't panic as there is no problem: this is exactly the same character &, as it should be presented in any well-formed XML document.

You can see that this is exactly the same character, get the string value of the node and output it -- you'll see that just & is output.

Another way to ascertain that this is just the & character is in XSLT to output it when the output method is set to "text". Here is a small, complete example:

XML document:

<t>M &amp; M</t>

Transformation:

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

 <xsl:template match="/*">
  <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

Result:

M & M

Upvotes: 1

Related Questions