Re: [xsl] transforming xml data in cdata

Subject: Re: [xsl] transforming xml data in cdata
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 08 Nov 2009 22:24:46 -0500
At 2009-11-08 18:58 -0800, road speeder wrote:
Hi, I have the following xml data:
<Sale><Site>101</Site>
<Details><![CDATA[ <?xml version="1.0"?><items><item>desk</item><cost>10</cost></items> ]]>
</Details>
</Sale>


I have an xslt "my.xslt" that tranforms the elements above except cdata part to result.xml. e.g <SalesItem><Location>101</Location></SalesItem>
However, I also have "another.xslt" that can transform the xml part within the cdata section.
I would like to import "another.xslt" into my.xslt and be able to transform the xml document within cdata as well.
With the result looking similar to the following:
<SalesItem><Location>101</Location><SkuDetails>
<![CDATA<?xml version="1.0"?><skus><sku>desk</sku><amount>10</amount></skus>]]>
</SkuDetails></SalesItem>


Any ideas would be appreciated if this can be accomplished.

Without using any proprietary extensions, I suggest you write out the <Details> element to a text file, transform it as an XML file, and then in a second pass on the first file pull in the transformed result again as a text file.


Using standard XSLT you cannot parse character data using template rules.

I hope this helps. The example stylesheets below produce the output you cite above without using any extensions, though I have no idea if it would meet any other requirements you aren't stating as I'm only using the one file for testing.

. . . . . . . . . . . . Ken


T:\ftemp>type speeder.xml
<Sale><Site>101</Site>
<Details><![CDATA[<?xml version="1.0"?><items><item>desk</item><cost>10</cost></items> ]]>
</Details>
</Sale>


T:\ftemp>call xslt2 speeder.xml speeder1.xsl temp.xml

T:\ftemp>type temp.xml
<?xml version="1.0"?><items><item>desk</item><cost>10</cost></items>

T:\ftemp>call xslt2 temp.xml speeder2.xsl temp2.xml

T:\ftemp>type temp2.xml
<?xml version="1.0" encoding="UTF-8"?><skus><sku>desk</sku><amount>10</amount></skus>
T:\ftemp>call xslt2 speeder.xml speeder3.xsl speeder.out.xml "detailsfile=temp2.xml"


T:\ftemp>type speeder.out.xml
<?xml version="1.0" encoding="UTF-8"?><SalesItem><Location>101</Location>
<SkuDetails><![CDATA[<?xml version="1.0" encoding="UTF-8"?><skus><sku>desk</sku><amount>10</amount></skus>]]></SkuDetails>
</SalesItem>
T:\ftemp>type speeder1.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">


<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:value-of select="Sale/Details"/>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>type speeder2.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:template match="items">
  <skus>
    <xsl:apply-templates/>
  </skus>
</xsl:template>

<xsl:template match="item">
  <sku>
    <xsl:apply-templates/>
  </sku>
</xsl:template>

<xsl:template match="cost">
  <amount>
    <xsl:apply-templates/>
  </amount>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>type speeder3.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:param name="detailsfile"/>

<xsl:output cdata-section-elements="SkuDetails"/>

<xsl:template match="Sale">
  <SalesItem>
    <xsl:apply-templates/>
  </SalesItem>
</xsl:template>

<xsl:template match="Site">
  <Location>
    <xsl:apply-templates/>
  </Location>
</xsl:template>

<xsl:template match="Details">
  <SkuDetails>
    <xsl:value-of select="unparsed-text($detailsfile)"/>
  </SkuDetails>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>rem Done!


-- Upcoming: hands-on XSLT, XQuery and XSL-FO Washington DC Nov 2009 Interested in other classes? http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread