Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2006, 03:31 AM
Registered User
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with the CData Section while using Xslt

Hi,

I have an Xml that contains a CData section. This CData section is usually used to store Xml elements of its own, but it was separated so that it would be able to store any kind of Xml (and to keep it general, because in one of the days we may want it to keep anything other than Xml).

The problem is when I'm using an Xslt and I'm trying to retreive the data in the CData section. I keep getting it with escaped characters. For example, if my Xml is:

<ROOT>
      <Node1>Value</Node1>
      <![CData[
           <InnerXmlElement>Hello World</InnerXmlElement>
      ]]>
</ROOT>

and the Xslt is (without all the headers...):

<InnerXml>
         <xsl:value-of select="ROOT/InnerXmlElement" />
</InnerXml>

The received xml is:

<InnerXml>
&lt;InnerXmlElement&gt;Hello World&lt;/InnerXmlElement&gt;</InnerXml>

I heard there is an attribute called disable-output-escaping and I tried to use that and it still didn't work. I also tried to change the output method of the Xslt to "text" but it didn't work either. I've been searching for an answer for days now! Can anyone help me?

Thanks,
Vered.


 
Old September 15th, 2006, 03:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is a design that's used quite often, though in my view it's a really bad one. XML is designed to be nested hierarchically. The purpose of a CDATA section is to say "the thing in here might look like markup, but I want it treated as ordinary text - don't treat angle brackets differently from any other character". So that's what XSLT does, of course, it treats it as text. You might have something that says <XmlInnerElement> but it's not an element at all: the very purpose of the CDATA section is to tell the recipient not to treat it as an element.

If you can't change the design, then there are two possible ways forward. If you only want to copy the text to the output, but treating it as markup this time, then disable-output-escaping might rescue you - but it's an optional feature that doesn't work in all circumstances, because it requires a special interface between the XSLT processor and the serializer. The other option is to extract the text of the CDATA section and pass it to an XML parser. You can do this in Saxon with the saxon:parse extension function, in another processor you might be able to do it by calling Javascript extensions.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 21st, 2006, 11:21 AM
Registered User
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thank's for your answer.

This is how we meant to design it, for the simple reason that the section we put in the CData section is a kind of input from the user gives us, unlike the rest of the xml, and we didn't want the entire xml to fail in validation if the inner xml is invalid.

However, If you have any suggestion about changes in design that handles this problem, I would love to hear it.

Can you tell me a little bit more about the second option? Because the disable-output-escaping didn't work, and I would like to try it, but I don't know what's saxon.

Thanks,
Vered.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Add a CDATA section; add nodeset to CDATA section kssudhish XSLT 3 January 3rd, 2008 07:13 AM
CDATA Section using XSLT misanaka XSLT 2 March 29th, 2007 06:53 PM
XSD CDATA Section chrisjonmcdonald XML 4 July 12th, 2006 08:15 AM
Load CDATA Section into DataSet monuindia2002 XML 1 April 25th, 2006 05:14 AM
CDATA section kai77 Beginning VB 6 0 October 31st, 2004 04:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.