|
Subject:
|
Problem with the CData Section while using Xslt
|
|
Posted By:
|
Vered
|
Post Date:
|
9/15/2006 3:31:16 AM
|
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> <InnerXmlElement>Hello World</InnerXmlElement></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.
|
|
Reply By:
|
mhkay
|
Reply Date:
|
9/15/2006 3:43:46 AM
|
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
|
|
Reply By:
|
Vered
|
Reply Date:
|
9/21/2006 11:21:05 AM
|
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.
|