XSL transform problem

  • Thread starter bhushan.kharabe
  • Start date
B

bhushan.kharabe

Hi,
I am stuck in problem involving transformation where the tree structure
of the target is different than the tree structure of the source.

My input xml looks like


<?xml version="1.0" encoding="utf-8" ?>
<tree>
<treeInfo></treeInfo>
<fruitInfo id="1"></fruitInfo>
<fruitInfo id="2"></fruitInfo>
</tree>


My target xml should look like


<?xml version="1.0" encoding="utf-8" ?>
<tree>
<treeInfo>
<fruits>
<fruitInfo id="1"></fruitInfo>
<fruitInfo id="2"></fruitInfo>
</fruits>
</treeInfo>
</tree>


How can I achieve this transformation using XSLT?


I have tried but the closest output I am able to get is


<?xml version="1.0" encoding="UTF-8"?>
<tree>
<treeInfo/>
<fruits>
<fruitInfo id="1"/>
<fruitInfo id="2"/>
</fruits>
</tree>


Any ideas how I can achieve my goal??


Thanks in advance for all the help.


Regards,
Bhushan
 
M

Martin Honnen

How can I achieve this transformation using XSLT?

<xsl:eek:utput method="xml" indent="yes" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="treeInfo">
<xsl:copy>
<xsl:apply-templates select="@*" />
<fruits>
<xsl:apply-templates select="following-sibling::fruitInfo" />
</fruits>
</xsl:copy>
</xsl:template>

<xsl:template match="tree">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="*[not(self::fruitInfo)]" />
</xsl:copy>
</xsl:template>
 
B

bhushan.kharabe

Thanks a ton Martin, it was a great help.

Regards,
Bhushan
Martin said:
How can I achieve this transformation using XSLT?

<xsl:eek:utput method="xml" indent="yes" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="treeInfo">
<xsl:copy>
<xsl:apply-templates select="@*" />
<fruits>
<xsl:apply-templates select="following-sibling::fruitInfo" />
</fruits>
</xsl:copy>
</xsl:template>

<xsl:template match="tree">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="*[not(self::fruitInfo)]" />
</xsl:copy>
</xsl:template>
 
P

Peter Flynn

Hi,
I am stuck in problem involving transformation where the tree structure
of the target is different than the tree structure of the source.

My input xml looks like


<?xml version="1.0" encoding="utf-8" ?>
<tree>
<treeInfo></treeInfo>
<fruitInfo id="1"></fruitInfo>
<fruitInfo id="2"></fruitInfo>
</tree>


My target xml should look like


<?xml version="1.0" encoding="utf-8" ?>
<tree>
<treeInfo>
<fruits>
<fruitInfo id="1"></fruitInfo>
<fruitInfo id="2"></fruitInfo>
</fruits>
</treeInfo>
</tree>


How can I achieve this transformation using XSLT?

Manage it from above:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="tree">
<tree>
<treeInfo>
<fruits>
<xsl:copy-of select="fruitInfo"/>
</fruits>
</treeInfo>
</tree>
</xsl:template>

</xsl:stylesheet>

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top