Search code examples
xmlxsltxslt-1.0cdata

inserting HTML into CDATA section via xsl:value-of select


I have a source XML that contains a tag with HTML. It was created from a clumsy CSV file.

The goal is to transform the source XML into a second XML Using the following,

<Description type="long" format="html">
  <![CDATA[             
    <xsl:value-of select="HTML_Descr"/>
 ]]>
</Description>

Unfortunately that XSL transforms as follows

<Description type="long" format="html">
  <![CDATA[             
    &lt;xsl:value-of select="HTML_Descr"/&gt;
 ]]>
</Description>

The output makes sense on reflection, but the goal is simply wrapping the HTML within CDATA.

NOTES: - It is not possible to put CDATA into the source XML. - More accurately, a source XML file is 100s of XML files - The processor is xsltproc, using XSL 1.0

Sorry. The copious helps found were simply preserving HTML format. Thanks in advance.

Addendum

The full process is CSV -> XML(temporary translation using CSV headers) -> XML (good) -> (X)HTML.

And the HTML cannot be translated from the temp XML because the good XML is maintained in a repository -- and updated on an ongoing basis.


Solution

  • Actually here is the closest question, Convert 'embedded' XML doc into CDATA output in XSLT (1.0)

    And answer:

    The following functions as desired, although it may not be the only and best solution.

    <xsl:template match="document">
      <document>
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <xsl:copy-of select="./html"/>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
      </document>
    </xsl:template>