user2363332
user2363332

Reputation: 25

Combine XML element nodes through xslt transformation

I need to transform a XML where transformation rules should be applied only for certain elements and rest should be copied same as it is. Below are source and expected xml blocks.

<Object class="Item" version="1.0" distName="A-1/B-1/Item-0">
     <p name="sDate">2013-02-11T00:00:00+02:00:00</p>
     <p name="present">1</p>
     <p name="stopD">2013-02-21T00:00:00+02:00:00</p>
     <p name="id">CPU</p>
</Object>
<Object class="Period" version="1.0" distName="A-1/B-1/Item-0/Period-0">
     <p name="sHour">0</p>
     <p name="sMinute">0</p>
     <p name="interval">1</p>
     <p name="day">0</p>
</Object>

to

<Object class="Items" distName="A-1/B-1/Items-0">
<p name="IsPresent">1</p>
<p name="StartDate">08-11-2012</p>
<p name="StopDate">29-11-2012</p>
<list name="TimePeriod">
    <item>
        <p name="id">1</p>
        <p name="StartTime">00:00</p>
        <p name="day">0</p>
        <p name="interval">15</p>
    </item>
</list>
<list name="TypeRef">
    <p>Diameter</p>
</list>

1. Above the Object element should be combined with another Object element based on distName having a parent child hierarchy. Eg : A-1/B-1/Item-0 with A-1/B-1/Item-0/Period-0

  1. Mapping as below:
    • sDate to StartDate
    • present to IsPresent
    • stopD to StopDate
    • id to TypeRef list
    • The other source node should be mapped to Timeperiod List

Can anyone help me out with this??

Upvotes: 1

Views: 130

Answers (1)

Kevin Brown
Kevin Brown

Reputation: 8857

Some of your items in your expected results do not seem to match with inputs, but maybe this would help you to get from point A to point B. I have not done date conversion here and I have no idea what TypeRef is in your source.

      <xsl:template match="Object[@class='Item']">
            <xsl:variable name="mainobj" select="."/>
            <xsl:variable name="distName" select="@distName"/>
            <Object class="Items" distName="{$distName}">
                <p name="IsPresent">
                    <xsl:value-of select="p[@name='present']"/>
                </p>
                <p name="StartDate">
                    <xsl:value-of select="p[@name='sDate']"/>
                </p>
                <p name="StopDate">
                    <xsl:value-of select="p[@name='stopD']"/>
                </p>
                <xsl:variable name="objperiod" select="//Object[starts-with(@distName,$distName)][not(.=$mainobj)]"/>
                <xsl:for-each select="$objperiod">
                <list name="TimePeriod">
                    <item>
                        <p name="id">
                            <xsl:value-of select="p[@name='id']"/>
                        </p>

                        <p name="StartTime">
                            <xsl:value-of select="concat(format-number($objperiod/p[@name='sHour'],'00'),':',format-number($objperiod/p[@name='sMinute'],'00'))"/>
                        </p>
                        <p name="day">
                            <xsl:value-of select="$objperiod/p[@name='day']"/>
                        </p>
                        <p name="interval">
                            <xsl:value-of select="$objperiod/p[@name='interval']"/>
                        </p>
                    </item>
                </list>
                </xsl:for-each>
            </Object>
        </xsl:template>  

This yields the following:

   <Object class="Items" distName="A-1/B-1/Item-0">
       <p name="IsPresent">1</p>
       <p name="StartDate">2013-02-11T00:00:00+02:00:00</p>
       <p name="StopDate">2013-02-21T00:00:00+02:00:00</p>
       <list name="TimePeriod">
           <item>
               <p name="id">CPU</p>
               <p name="StartTime">00:00</p>
               <p name="day">0</p>
               <p name="interval">1</p>
           </item>
       </list>
       <list name="TimePeriod">
           <item>
               <p name="id"/>
               <p name="StartTime">00:00</p>
               <p name="day">0</p>
               <p name="interval">1</p>
           </item>
       </list>
       <list name="TimePeriod">
           <item>
               <p name="id"/>
               <p name="StartTime">00:00</p>
               <p name="day">0</p>
               <p name="interval">1</p>
           </item>
       </list>
   </Object>

Upvotes: 1

Related Questions