Reputation: 3738
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[
<xsl:value-of select="HTML_Descr"/>
]]>
</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.
Upvotes: 0
Views: 10075
Reputation: 3738
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"><![CDATA[</xsl:text>
<xsl:copy-of select="./html"/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</document>
</xsl:template>
Upvotes: 4
Reputation: 60190
You probably want this:
<xsl:value-of select="HTML_Descr" disable-output-escaping="yes"/>
Note, however, that this may render your XML file invalid if the HTML fragment in the CDATA is no proper XML (for instance XHTML). Use with caution - it's a strong code smell!
Better way would be to have the HTML as real XHTML in its own namespace in the XML file instead of using CDATA literal text blocks, so that you could just have the XSLT processor copy the nodes instead of using the disable-output-escaping
"hack".
Upvotes: 0