Simple way to group heavy data in xslt 1.0 using non Muenchian grouping



I am a total newbie on xslt, and hence unable to understand the complicated solutions provided here for what I want to achieve. Grouping of a heavy data structure ( meaning there are lots of attributes and elements and hence cannot be going with any concatenated solution) in a non Muenchian way. Coz I feel concatenation of large data will hit performance. Here is my sample input data, required output and xsl I have tried :


# input xml #



<out:OuterSegment>
<out:Segment >
<out:from Code="CHN"/>
<out:tothis Code="HYD"/>
<out:group>0</out:group>
</out:Segment>
<out:Segment >
<out:from Code="HYD"/>
<out:tothis Code="BLR"/>
<out:group>1</out:group>
</out:Segment>
<out:Segment >
<out:from Code="BLR"/>
<out:tothis Code="TVN"/>
<out:group>2</out:group>
</out:Segment>
<out:Segment >
<out:from Code="TVN"/>
<out:tothis Code="DEL"/>
<out:group>2</out:group>
</out:Segment>
</out:OuterSegment>


output xml



<out2:OuterSegment>
<out2:Segment >
<out2:from Code="CHN"/>
<out2:tothis Code="HYD"/>
<out2:group>0</out2:group>
</out2:Segment>
</out2:OuterSegment>
<out2:OuterSegment>
<out2:Segment >
<out2:from Code="HYD"/>
<out2:tothis Code="BLR"/>
<out2:group>1</out2:group>
</out2:Segment>
</out2:OuterSegment>
<out2:OuterSegment>
<out2:Segment >
<out2:from Code="BLR"/>
<out2:tothis Code="TVN"/>
<out2:group>2</out2:group>
</out2:Segment>
</out2:OuterSegment>
<out2:OuterSegment>
<out2:Segment >
<out2:from Code="TVN"/>
<out2:tothis Code="DEL"/>
<out2:group>2</out2:group>
</out2:Segment>
</out2:OuterSegment>


XSL snippet



<xsl:choose>
<xsl:when test="position() = 1">
<xsl:message> its the first row </xsl:message>
<out:OuterSegment>
<xsl:apply-templates select="in2:IncomingSegment"
mode="localIncomingSegmentRefToSegment" />
</out:OuterSegment>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="prevgroup" select="//in2:IncomingSegment[position() - 1]/@Group"/>
<xsl:message> is the subsequent row</xsl:message>
<xsl:if test="$prevgroup = //in2:IncomingSegment[$prevgroup]/@Group">
<xsl:apply-templates select="in2:IncomingSegment"
mode="localIncomingSegmentRefToSegment" />
</xsl:if>
<xsl:if test="$prevgroup != //in2:IncomingSegment[$prevgroup]/@Group">
<out:OuterSegment>
<xsl:apply-templates select="in2:IncomingSegment"
mode="SegmentRefToSegment" />
</out:OuterSegment>
</xsl:if>
</xsl:otherwise>
</xsl:choose>


I will need to mention that the above input xml was generated by another xslt for which the input is as :


Actual input xml



<in:RefSegment key="1" group="0">
<in:inSegment >
<in:from Code="CHN"/>
<in:tothis Code="HYD"/>
</in:inSegment>
</in:RefSegment>
<in:RefSegment key="2" group="1">
<in:inSegment >
<in:from Code="HYD"/>
<in:tothis Code="BLR"/>
</in:inSegment>
</in:RefSegment>
<in:RefSegment key="3" group="2">
<in:inSegment >
<in:from Code="BLR"/>
<in:tothis Code="TVN"/>
</in:inSegment>
</in:RefSegment>
<in:RefSegment key="4" group="2">
<in:inSegment >
<in:from Code="TVN"/>
<in:tothis Code="BLR"/>
</in:inSegment>
</in:RefSegment>
<in:DataSegments>
<in:Data >
<in:keyref> 0 </in:keyref>
</in:Data>
<in:Data >
<in:keyref> 1 </in:keyref>
</in:Data>
<in:Data >
<in:keyref> 2 </in:keyref>
</in:Data>
<in:Data >
<in:keyref> 2 </in:keyref>
</in:Data>
<in:DataSegments>


Is it possible to obtain the final output.xml in one shot out of the above actual input.xml ?


No comments:

Post a Comment