Subject:how to get xsl:copy-of to appear within CDATA Author:Dan R Date:10 Oct 2007 08:49 AM Originally Posted: 10 Oct 2007 08:47 AM
I'm using an xsl:for-each on a collection of documents.
I am merging many legacy xml files into a single new output xml file.
My legacy xml files contain no cdata, however my new output xml file format requires CDATA to encapsulate various element's content.
My legacy xml files contain a section of xhtml.
in my xsl:output statement, I have set cdata-section-elements="newstuff"
for elements I'm creating, this:
<xsl:element name="newstuff">
<xsl:text>hello</xsl:text>
</xsl:element>
outputs successfully to this:
<newstuff><![CDATA[hello]]></newstuff>
HOWEVER, the problem comes when I try to say this:
<xsl:element name="newstuff">
<xsl:copy-of select="oldhtml">
</xsl:element>
I get this:
<newstuff><p>hello</p></newstuff>
I really want to get this:
<newstuff><![CDATA[<p>hello</p>]]></newstuff>
Why is the cdata-section-elements being ignored when i do a copy-of?
Do I need to convert the copy-of result to a string? how do I convert it to text while still preserving the xhtml markup?
Subject:how to get xsl:copy-of to appear within CDATA Author:(Deleted User) Date:10 Oct 2007 11:24 AM
Hi Dan,
the XSLT specs state that "cdata-section-elements specifies a list of the names of elements whose text node children should be output using CDATA sections" (note the "whose TEXT node children" part), so it doesn't do what you are expecting. What you want is to serialize a number of XML elements into a text value, and encapsulate that text into a CDATA section in order to avoid escaping. The only thing that comes to my mind is to use an extension function to perform the serialization, instead of using xsl:copy-of; if you are using Saxon, you can use saxon:serialize().
Subject:how to get xsl:copy-of to appear within CDATA Author:Dan R Date:10 Oct 2007 12:38 PM
Thank you Alberto.
Unfortunately, I am having difficulty implementing the saxon:serialize function.
It would be helpful to have a working XSLT example, as one was not provided in the documentation I read here: http://saxonica.com/documentation/extensions/functions/serialize.html