XSLT output CDATA sections

I was transforming some xml to xml another format and some of the text nodes could potentially contained invalid characters, so placing them in CDATA sections would be wise. I tried the following –

[code lang=”xml”]

…….

< ![CDATA[
]]>

[/code]

which of course did not work and resulted in what you see below. I knew this would happen but I was real busy and figured it worth a shot.

[code lang=”xml”]

<xsl:value-of select=”fields/field[normalize-space(@name)=’Description’]/value/text()” />

[/code]

Some digging resulted in the finding of the “cdata-section-elements” attribute which contains a whitespace-separated list of QNames so using –

[code lang=”xml”]

…….



[/code]

I get the desired result

[code lang=”xml”]
< ![CDATA[This is a description]]>
[/code]

Marvellous I tell you, It’s amazing what you can actually learn when you RTFM , in this case the XSL W3C recommendation.