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 June 15th, 2011, 07:38 AM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default setting xml as CData

I want to include in my xml response an element that must contain a chunk of someother xml as Cdata:

For example:
Code:
	<Extras>
			<Extra>
				<AdditionalInfo><![CDATA[
				<baggagePrices paxTypes="ADT,CHD" rulesPerDirection="false">
					<baggagePrice currency="EUR" description="0 Bags &amp; Online Check-in" id="0" maxTotalWeight="15" maxTotalWeightUnit="kg" pieces="0" price="0.00"/>
				
				</baggagePrices>
				]]></AdditionalInfo>
			</Extra>
		</Extras>
In the above example the element "AdditionalInfo" shoudl contain "baggagePrices" as CData.

I have tried with the following code:

Code:
	<xsl:template match="baggagePrices">
		<Extra>
		<xsl:text>&lt;![CDATA[</xsl:text>
		<AdditionalInfo>
				<xsl:copy-of select="."/>
		<AdditionalInfo>		
		<xsl:text>]]&gt;</xsl:text>
		</Extra>
	</xsl:template>
But the response includes &lt;![CDATA ..]]&gt; instead of <![CDATA ..]]>

I have also tried
Code:
 	<xsl:output encoding="UTF-8" version="1.0" method="xml" indent="yes" cdata-section-elements="baggagePrices"/>
but then Cdata is not appearing and the xml is copied as it is.

Pls help.
 
Old June 15th, 2011, 07:51 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If your XSLT processor supports "disable-output-escaping" it you can try to hack that with
Code:
  <AdditionalInfo><xsl:text disable-output-escaping="yes">&lt;[CDATA[</xsl:text>
     <xsl:copy-of select="."/>
  <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text></AdditionalInfo>
A cleaner solution would be to use cdata-section-elements="AdditionalInfo" and then
Code:
  <AdditionalInfo><xsl:value-of select="saxon:serialize(.)"/></AdditionalInfo>
where saxon:serialize is http://www.saxonica.com/documentatio.../serialize.xml.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 15th, 2011, 07:56 AM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i am using xslt 1.0
 
Old June 15th, 2011, 08:05 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Which XSLT 1.0 processor do you use? Perhaps it has an extension function similar to saxon:serialize or it allows implementing such functions.
And my first suggestion with the disable-output-escaping hack does not depend on the XSLT version but rather on the XSLT processor serializing the output and supporting disable-output-escaping while doing that.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 15th, 2011, 08:20 AM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i am usign Xalan.
 
Old June 15th, 2011, 08:25 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't use Xalan, according to http://xml.apache.org/xalan-j/extensionslib.html the Java version of Xalan does not seem to have an extension function to serialize a node. So you will have to try the CDATA hack. Or you could use a pure XSLT implementation of serializing a node, like http://lenzconsulting.com/xml-to-string/.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 15th, 2011, 08:46 AM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i did this

Code:
<xsl:template match="baggagePrices">
		<Extra>
		
		<AdditionalInfo>
		  <!--xsl:copy-of select="."/-->
		  <xsl:call-template name="xml-to-string">
				<xsl:with-param name="baggage" select="."/>
		  </xsl:call-template>
		  
		</AdditionalInfo>		
		
		</Extra>
	</xsl:template>
but now getting lots of .CourierMarshalUnmarshalExceptions...

By using <xsl:import href="xml-to-string.xsl"/>

Last edited by sbutt; June 15th, 2011 at 08:48 AM..
 
Old June 15th, 2011, 09:34 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I have made two examples that work fine for me with Xalan 2.7.1 Java.
The input XML is as follows:
Code:
<root>
  <foo>
    <bar>
      <baz>a > b</baz>
    </bar>
  </foo>
</root>
The first XSLT sample uses the CDATA output "hack" with disable-output-escaping:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:template match="@* | node()">
    <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="foo">
    <xsl:copy>
      <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
      <xsl:copy-of select="node()"/>
      <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
With that Xalan outputs
Code:
<?xml version="1.0" encoding="UTF-8"?><root>
  <foo><![CDATA[
    <bar>
      <baz>a &gt; b</baz>
    </bar>
  ]]></foo>
</root>
The second stylesheet imports the one I linked to and uses cdata-section-elements="foo":
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:import href="http://lenzconsulting.com/xml-to-string/xml-to-string.xsl"/>
  
  <xsl:output method="xml" cdata-section-elements="foo"/>
  
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
   
  <xsl:template match="@* | node()">
    <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="foo">
    <xsl:copy>
      <xsl:call-template name="xml-to-string">
        <xsl:with-param name="node-set" select="node()"/>
      </xsl:call-template>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
With that Xalan outputs
Code:
<root>
  <foo><![CDATA[
    <bar>
      <baz>a > b</baz>
    </bar>
  ]]></foo>
</root>
So far as far as I can tell both suggested approaches solve the problem.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 15th, 2011, 10:24 AM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

unfortunately it never worked on my side:(

Code:
	<xsl:import href="http://lenzconsulting.com/xml-to-string/xml-to-string.xsl"/>
	
  <xsl:output method="xml" cdata-section-elements="AdditionalInfo"/>

...

<xsl:template match="baggagePrices">
		<Extra>
			<AdditionalInfo>
				<xsl:copy>
					<xsl:call-template name="xml-to-string">
						<xsl:with-param name="node-set" select="node()"/>
					</xsl:call-template>
				</xsl:copy>
			</AdditionalInfo>
		</Extra>
	</xsl:template>
much thanks anyway.
 
Old June 15th, 2011, 10:30 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well you will have to decide what you want, if you want the "AdditionalInfo" element to contain a CDATA then don't put any element nodes in there, as you currently do with your xsl: copy.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Format cdata text into xml Alisha XSLT 1 January 3rd, 2011 07:23 AM
How to strip out CDATA from xml with XSLT XSLUSer XSLT 10 March 17th, 2009 11:28 AM
how to strip out CDATA from xml with xslt XSLUSer XSLT 0 March 10th, 2009 11:47 PM
SQL Server 2005 XML: FOR XML PATH -> cdata? stoves SQL Server 2005 1 July 8th, 2008 02:40 AM
Extract the XML file from CDATA haixia XSLT 2 August 3rd, 2006 02:01 PM





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