0

I am using Saxon to perform XSLT transform in .Net application. This is my .net code

    private void xsltTransform()
    {
        string xsltFile = Path.Combine(TEirPath, "test.xsl");
        string outputFile = Path.Combine(TEirPath, "test.html");

        var xslt = new FileInfo(xsltFile);

        var processor = new Processor();
        var xsltCompiler = processor.NewXsltCompiler();

        var xsltExecutable = xsltCompiler.Compile(new Uri(xslt.FullName));

        var destination = new DomDestination();
        var xsltTransformer = xsltExecutable.Load();

        xsltTransformer.InitialTemplate = new QName("startTemplate");
        xsltTransformer.Run(destination);

        destination.XmlDocument.Save(outputFile);
    }

And this is my sample test.xsl

<xsl:stylesheet version ="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xhtml" encoding="UTF-8"/>

<xsl:template name="startTemplate" > 
<a>
    <xsl:text disable-output-escaping="no">Test1 &lt; &gt; Test1</xsl:text>
    <br/>
    <xsl:text disable-output-escaping="yes">Test2 &lt; &gt; Test2</xsl:text>
</a>
</xsl:template> 

</xsl:stylesheet>

I need to have the output to display special characters (<>) in both escaped and non-escaped forms. But from the above xsl sample, the result that i get in test.html is always escaped. I think the problem is with the Destination being output as XmlDocument, but i don't know how to get around it. Could someone give me suggestion? Thank you in advance.

4
  • I tested it with saxon9he.jar and the output is as expected: <a>Test1 &lt; &gt; Test1 <br></br>Test2 < > Test2. If you really want to disregard disable-output-escaping you have to use a browser like Firefox, because they still haven't implemented it ;-) Commented Mar 27, 2018 at 20:51
  • So why are you using a DomDestination, do you need a System.Xml.XmlDocument as the transformation result? If you want doe to have any effect then I think you need to use saxonica.com/html/documentation/dotnetdoc/Saxon/Api/… over a Stream or TextWriter. But no such result like <a>test < > test <a/> can later be loaded by XmlDocument as it is meant to parse XML and an unescaped less than sign is not XML. Commented Mar 27, 2018 at 21:17
  • I don't need to use DomDestination or XmlDocument specifically. In fact, I want the result as html to be converted into pdf in a later process. But as i mentioned.. i don't know how to get around it. Martin, can you please elaborate on the usage of Serializer? i briefly read the documentation from the link you provide, it is outputting xml, so i still don't think it would help me. Commented Mar 27, 2018 at 22:05
  • I found out how to use Serializier from stackoverflow.com/questions/2443704/…, and it works. Thank you, Martin. Commented Mar 27, 2018 at 22:46

1 Answer 1

1

disable-output-escaping affects the actions of the XSLT serializer. If the XSLT processor is not performing serialization, then it has no effect. In your case serialization is being done using XmlDocument.Save, which has no knowledge of anything in the XSLT stylesheet.

If you want to use disable-output-escaping or other XSLT serialization options then you need to send the transformation output to a (Saxon) Serializer.

Having said that, 9 times out of 10 when people use disable-output-escaping there is a better way to solve the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

I got it to work using Serializer. I find how to use Serializier here: stackoverflow.com/questions/2443704/…. For my case, i set Serializer.Method to "html". Thank you for your help, Michael, and Martin also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.