xsl-list
[Top] [All Lists]

Re: [xsl] transforming xml data in cdata

2009-11-09 03:44:06
What happens (as is likely) when there is more than one <SkuDetails>
element in the original XML? I guess they'll all be written to the
same temp.xml and finally be all inserted (in translated form) in all
<SkuDetails> elements. Although I guess a unique filename could be
generated for each <SkuDetails>, this will make the translation step
more complicated, but easily solvable on UNIX (I've absolutely
forgotten the bat-ty commands).

Since a most experienced contributor on this list has suggested using
Perl or sed for a similar problem, I'll do so for this problem, too.

Below is the set of sed commands that would achieve the renaming, and
the sed command to do the transformation

$ cat sale.sed
\|\(</\{0,1\}\)item>|  s//\1sku>/g
\|\(</\{0,1\}\)items>| s//\1skus>/g
\|\(</\{0,1\}\)cost>|  s//\1amount>/g
\|\(</\{0,1\}\)Sale>|  s//\1SalesItem>/g
\|\(</\{0,1\}\)Site>|  s//\1Location>/g

$  sed -f sale.sed sale.xml >sale1.xml

If you are on Windows, you could install Perl (from Activestate),
which will give you psed, a highly compatible Perl implementation of
sed.

-W

On Mon, Nov 9, 2009 at 4:24 AM, G. Ken Holman
<gkholman(_at_)cranesoftwrights(_dot_)com> wrote:

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(_at_)CraneSoftwrights(_dot_)com
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>