Re: [xsl] enclose output of transform in CDATA section

Subject: Re: [xsl] enclose output of transform in CDATA section
From: Armen Martirossian <armmarti@xxxxxxxxx>
Date: Thu, 14 Aug 2003 05:43:55 -0700 (PDT)
--- Dave Beattie <Dave.Beattie@xxxxxxxxxxx> wrote:
> Hi
> 
> I have to produce an output file in the following
> format
> 
> <Message>  
> 	<![CDATA[
> 		...xml document as generated from a transform in
> here ...
> 	]]> 
> </Message>
> 
> Any suggestions as to how I go about doing this.
> 
> Thanks
> 
> 
> Dave
>
I can suggest to use xsl:text with
disable-output-escaping="yes", though as discussed
many times the feature(d-o-e) is not supported by all
XSLT processors and must be avoided if possible.

However, to perform an identity transformation for
example, and put the result in the CDATA section, you
can do this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<Message>
			<xsl:text
disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
				<xsl:apply-templates/>
			<xsl:text
disable-output-escaping="yes">]]&gt;</xsl:text>
		</Message>
	</xsl:template>
	
	<xsl:template match="node() | @*">
		<xsl:copy>
			<xsl:apply-templates select="node() | @*"/>
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>


But there is another potential problem here: in spite
of how you output(or generate) CDATA delimiters here,
the source XML can contain "]]>" sequence or even
during the transformation such sequence may be
generated and the result will not be well-formed
XML(CDATA section will contain forbidden "]]>"
sequence)...

Regards,
Armen

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread