XML Development

Moderators: None (Apply to moderate this forum)
Number of threads: 252
Number of posts: 451

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
transform XML to XML Posted by sgoldberg on 25 Aug 2008 at 8:55 AM
I have XML that I need to tranform to another structure of XML. The XML I need to transform is below:

<ArrayNode>
<Base>
<aa>1</aa>
<bb>text</bb>
<cc>
<ccc>
<cccc>text#1</cccc>
<cccc2>text#1</cccc2>
</ccc>
<ccc>
<cccc>text#2</cccc>
<cccc2>text#2</cccc2>
</ccc>
</cc>
<dd>text</dd>
</Base>
<Base>
<aa>2</aa>
<bb>text2</bb>
<cc>
<ccc>
<cccc>text#3</cccc>
<cccc2>text#3<cccc2>
</ccc>
<dd>text</dd>
</Base>
</ArrayNode>


This needs be tranformed to:

<ArrayNode>
<Base>
<aa></aa>
<bb></bb>
<cc>
<ccc>
<cccc>Text#1</cccc>
<cccc2>Text#1</cccc2>
</ccc>
</cc>
<dd></dd>
</Base>
<Base>
<aa></aa>
<bb></bb>
<cc>
<ccc>
<cccc>Text#2</cccc>
<cccc2>Text#2</cccc2>
</ccc>
</cc>
<dd></dd>
</Base>
<Base>
<aa></aa>
<bb><bb>
<cc>
<ccc>
<cccc>Text#3</cccc>
<cccc2>Text#3<cccc2>
</ccc>
<cc>
<dd></dd>
</Base>
</ArrayNode>

Wherever <ccc> exists, it needs to loop over each <ccc> and insert each individual <ccc> into the rest of the data for that <Base> group. So if there are 3 <ccc>'s, it should repeat the entire <Base> group and insert one of the <ccc> nodes.

Thanks in advance
Report
Re: transform XML to XML Posted by jeet1212 on 25 Aug 2008 at 11:26 PM
Hi,

Please try below code...I think it will help you.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="ArrayNode">
<xsl:element name="{name(.)}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Base">
<xsl:for-each select="child::*/ccc">
<xsl:element name="{name(ancestor::Base)}">
<xsl:copy-of select="../preceding-sibling::*"/>
<xsl:element name="{name(..)}">
<xsl:copy-of select="."/>
</xsl:element>
<xsl:copy-of select="../following-sibling::*"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Thanks!
Report
Re: transform XML to XML Posted by sgoldberg on 2 Sept 2008 at 8:01 AM
Thank you! That was perfect. I was trying to use "copy-of" and manipulate the for each after the copy was done. This is great xslt!
Report
Re: transform XML to XML Posted by mdabiri on 12 Nov 2008 at 12:56 PM
I also need to transform xml to xml but the result xml does not show as xml, I can only see the transformed values i,e in this example, I see Text#1 Text#1 on one line and Text#2 Text#2 on the second and not an xml file and when I view source, I only see the original xml doc.
Maybe not using the correct tranformation instruction. Your help is much appreciated..

This is the xml file
----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="arrayNode.xslt"?>
<ArrayNode>
<Base>
<aa></aa>
<bb></bb>
<cc>
<ccc>
<cccc>Text#1</cccc>
<cccc2>Text#1</cccc2>
</ccc>
</cc>
<dd></dd>
</Base>
<Base>
<aa></aa>
<bb></bb>
<cc>
<ccc>
<cccc>Text#2</cccc>
<cccc2>Text#2</cccc2>
</ccc>
</cc>
<dd></dd>
</Base>
</ArrayNode>
------------------------------------
And here is the xsl file called arraynode.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="ArrayNode">
<xsl:element name="{name(.)}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Base">
<xsl:for-each select="child::*/ccc">
<xsl:element name="{name(ancestor::Base)}">
<xsl:copy-of select="../preceding-sibling::*"/>
<xsl:element name="{name(..)}">
<xsl:copy-of select="."/>
</xsl:element>
<xsl:copy-of select="../following-sibling::*"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
-------------------------------
And here is the result of transformation.
Text#1 Text#1
Text#2 Text#2
But I want another xml file..




 

Recent Jobs