preserving CDATA in xml to xml translation

1,016 views
Skip to first unread message

Brent

unread,
Jul 11, 2008, 1:45:02 PM7/11/08
to XSLT
I'm trying to do an xml to xml translation. The input xml doc
contains some CDATA elements and I want to preserve the CDATA notation
in my output. I had thought that the xsl:output element would take
care of the CDATA notation in my output doc but it is not doing the
trick.

Here's a simplified example of what I'm trying to pull off. Please
let me know if you have any ideas how to do this.

Thanks,
Brent


Input:
<?xml version="1.0"?>
<Report>
<Author>Kilgore Trout</Author>
<Body><![CDATA[Ugly Text Report

Data Row One abcd
Date Row Two efg

The end.

]]>
</Body>
</Report>

Transformer:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">

<xsl:output method="xml" cdata-section-elements="Data"/>

<xsl:template match="/">
<Results>
<Data>
<xsl:copy-of select="Report/Body"/>
</Data>
</Results>
</xsl:template>
</xsl:stylesheet>

Output:
<?xml version='1.0' ?>
<Results><Data>Ugly Text Report

Data Row One abcd
Date Row Two efg

The end.


</Data></Results>


Desired Output:
<?xml version='1.0' ?>
<Results><Data><![CDATA[Ugly Text Report

Data Row One abcd
Date Row Two efg

The end.

]]>
</Data></Results>

Balaji

unread,
Jul 15, 2008, 5:13:51 AM7/15/08
to XSLT
Hi,

Use apply-templates instead of copy-of statement, it will work

<xsl:template match="/">
<Results>
<Data>
<xsl:apply-templates select="Report/Body"/>
</Data>
</Results>
</xsl:template>

Regards,
Balaji. M
Reply all
Reply to author
Forward
0 new messages