tags:

views:

25

answers:

1

Is it possble to do the transformation below using XSL 1.0. If yes can please post some sample code which can get me started in the right direction.

<Region> <RecType1><Amt> 100 </Amt></RecType1><RecType2><Name>XXX</Name></RecType2><RecType1><Amt> 200 </Amt></RecType1><RecType2><Name>YYY</Name></RecType2><RecType1><Amt> 300 </Amt></RecType1><RecType2><Name>ZZZ</Name></RecType2></Region>

TO

<Region> <Payment><Amt>100</Amt><Name>XXX</Name></Payment><Payment><Amt>200</Amt><Name>YYY</Name></Payment><Payment><Amt>300</Amt><Name>ZZZ</Name></Payment></Region>

I appreciate your help.

Thank you, Praveen

A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="RecType1">
   <Payment>
    <xsl:apply-templates select="* | following-sibling::RecType2[1]/*"/>
   </Payment>
 </xsl:template>

 <xsl:template match="RecType2"/>
</xsl:stylesheet>

when applied on the provided XML document (indented to be made readable):

<Region>
    <RecType1>
        <Amt> 100 </Amt>
    </RecType1>
    <RecType2>
        <Name>XXX</Name>
    </RecType2>
    <RecType1>
        <Amt> 200 </Amt>
    </RecType1>
    <RecType2>
        <Name>YYY</Name>
    </RecType2>
    <RecType1>
        <Amt> 300 </Amt>
    </RecType1>
    <RecType2>
        <Name>ZZZ</Name>
    </RecType2>
</Region>

produces the desired result (also indented to be readable):

<Region>
    <Payment>
        <Amt> 100 </Amt>
        <Name>XXX</Name>
    </Payment>
    <Payment>
        <Amt> 200 </Amt>
        <Name>YYY</Name>
    </Payment>
    <Payment>
        <Amt> 300 </Amt>
        <Name>ZZZ</Name>
    </Payment>
</Region>
Dimitre Novatchev