0

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.

1

2 Answers 2

4

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> 
Sign up to request clarification or add additional context in comments.

Comments

0

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".

6 Comments

thanks. that seems to be rendering and,or preserving the HTML. i have no control over the source. the HTML could contain invalid formatting. a common problem is not closing <p> tags or using <br> (instead of </br>. or perhaps i'm misunderstanding you.
@user1869322, is your target really another XML or just HTML? Are you aware that CDATA is just a different encoding of string but the value is represents is the same as when single entities are encoded?
my target ( of xsltproc ) is another XML. yes. otherwise, it would be a typical matter of disable-output-escaping and methods found in other answers. thnx.
@user1869322, in that case, why even bother with the CDATA? You can tell the XSLT processor which element contents should be rendered out (e.g. using <xsl:output cdata-section-elements="Description">) as CDATA. In your template, just do a <xslt:value-of ...> and don't try to manually the insert CDATA start or end.
ill look up that cdata-section-elements. nevertheless, eventually it will be HTML. the full process is CSV -> XML(temporary translation using CSV headers) -> XML (good) -> 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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.