XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Dan RSubject: 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?

Thanks.

Postnext
(Deleted User) 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().

Hope this helps,
Alberto

Postnext
Dan RSubject: 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

I am using saxon 8.8J and Java 1.6.0_02

I get XPST003: XPath syntax error: cannot find a matching 2-argument function named {http://saxon.sf.net}serialize().

I don't have much experience using extension functions.
I added the namespace to my xsl:stylesheet element:
xmlns:saxon="http://saxon.sf.net"

I wrote the call this way:
<xsl:value-of select="saxon:serialize((faqtoid/answer/*), 'text')" />

where the children of <answer> contain the xhtml I wish to serialize.
what am I doing wrong?

Thanks.

Postnext
(Deleted User) Subject: how to get xsl:copy-of to appear within CDATA
Author: (Deleted User)
Date: 10 Oct 2007 03:44 PM
Hi Dan,
the namespace contains an trailing '/' that you missed; it's xmlns:saxon="http://saxon.sf.net/"

Alberto

Posttop
Dan RSubject: how to get xsl:copy-of to appear within CDATA
Author: Dan R
Date: 10 Oct 2007 04:56 PM
Thank you very much Alberto, I'm back in business.

Here's what my code ended up looking like, for those who are interested:

xmlns:saxon="http://saxon.sf.net/

<xsl:output name="htmlanswer" encoding="utf-8" method="xhtml" indent="no" omit-xml-declaration="yes"/>

<xsl:variable name="answer">
<xsl:copy-of select="faqtoid/answer/*"></xsl:copy-of>
</xsl:variable>

<xsl:variable name="cdata">
<xsl:value-of select="saxon:serialize($answer, 'htmlanswer')"></xsl:value-of>
</xsl:variable>

<xsl:value-of select="normalize-space($cdata)"></xsl:value-of>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.