Neil
Neil

Reputation: 11889

How to get Saxon to output un-escaped CDATA section

How can I get Saxon to convert a CDATA string into an XdmNode without escaping the < & > ?

Here is my 'ToXdmNode' function:

        Processor processor = xxx.GetProcessor();
        var builder = processor.NewDocumentBuilder();

        builder.BaseUri = xxx.BaseUri;
        var tr = new StringReader("<stuff><![CDATA[<blah>]]></stuff>");
        var node = builder.Build(tr);

This code converts

<stuff><![CDATA[<blah>]]></stuff>

into an XdmNode that looks like:

<stuff>&lt;blah&gt;</stuff>

This causes problems later on when I send the OuterXml onto the next step.

How can I get my XdmNode to be unescaped?

Upvotes: 0

Views: 408

Answers (1)

Michael Kay
Michael Kay

Reputation: 163282

You say the code converts

<stuff><![CDATA[<blah>]]></stuff>

into an XdmNode that looks like:

<stuff>&lt;blah&gt;</stuff>

but actually that's not a conversion: the two things are just different serializations of the same content.

If you want to create the non-well-formed string

<stuff><blah></stuff>

then that's tricky using XSLT, because not being XML, this isn't the serialization of any valid XDM node. However, you can contrive it with the help of disable-output-escaping. For example the transformation

<xsl:template match="stuff">
  <xsl:copy>
    <xsl:value-of select="." disable-output-escaping="yes"/>
  </xsl:copy>
</xsl:template>

will produce this output provided that you send the transformation output to a Serializer (and not, for example, to an XdmNode).

Upvotes: 1

Related Questions