Group XML Key Tag with Same value using XSLT

Viewed 46

Need help here in one of the query regarding parsing XML code via XSLT. Below is an example of the XML code i've.

 <?xml version="1.0" encoding="UTF-8"?>
 <Entity>
   <Shipment>
        <shipFromLocation>06AC001</shipFromLocation>
        <shipToLocation>07BCDAZ</shipToLocation>
        <container>
            <key1>101</key1>
            <key1>102</key1>
        </container>
        <container>
            <key1>103</key1>
            <key1>104</key1>
        </container>
   </Shipment>
   <Shipment>
        <shipFromLocation>06AC001</shipFromLocation>
        <shipToLocation>07BCDAZ</shipToLocation>
        <container>
            <key1>105</key1>
            <key1>106</key1>
        </container>
        <container>
            <key1>107</key1>
            <key1>108</key1>
        </container>
   </Shipment>
   <Shipment>
        <shipFromLocation>06AC002</shipFromLocation>
        <shipToLocation>08BCDAZ</shipToLocation>
        <container>
            <key1>200</key1>
            <key1>201</key1>
        </container>
   </Shipment>
   <Shipment>
        <shipFromLocation>06AC002</shipFromLocation>
        <shipToLocation>08BCDAZ</shipToLocation>
        <container>
            <key1>202</key1>
            <key1>203</key1>
        </container>
   </Shipment>
</Entity>

As you can see there 4 tag, within each tag you have <shipFromLocation> and <shipToLocation>

  • The first 2 <Shipment> tag contains the same value for <shipFromLocation> and <shipToLocation>
  • The last 2 <Shipment> tag contains the same value for <shipFromLocation> and <shipToLocation>

Now within each <Shipment> tag there are multiple <container> tag. I'm trying to get a result as follows

  <?xml version="1.0" encoding="UTF-8"?>
 <Entity>
   <Shipment>
        <shipFromLocation>06AC001</shipFromLocation>
        <shipToLocation>07BCDAZ</shipToLocation>
        <container>
            <key1>101</key1>
            <key1>102</key1>
        </container>
        <container>
            <key1>103</key1>
            <key1>104</key1>
        </container>
        <container>
            <key1>105</key1>
            <key1>106</key1>
        </container>
        <container>
            <key1>107</key1>
            <key1>108</key1>
        </container>
   </Shipment>
   <Shipment>
        <shipFromLocation>06AC002</shipFromLocation>
        <shipToLocation>08BCDAZ</shipToLocation>
        <container>
            <key1>200</key1>
            <key1>201</key1>
        </container>
        <container>
            <key1>202</key1>
            <key1>203</key1>
        </container>
   </Shipment>

</Entity>

That is, you are grouping based on <shipFromLocation> and <shipToLocation> and aggregating all details in it. I'm failing to generate the XSLT code for it. The one that i've is a broken one

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>

<xsl:template match="/Entity">
    <xsl:copy>
        <xsl:for-each-group select="Shipment" group-by="shipFromLocation">
        <Shipment>
            <xsl:for-each select="current-group()">
                <shipFromLocation>
                    <xsl:value-of select="shipFromLocation"/>
                </shipFromLocation>
                
                
                    <container>
                        <xsl:value-of select="container"/>
                        
                    </container>

            
            </xsl:for-each>
        </Shipment>
     
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Can someone please do help me....

1 Answers

AFAICT, you want to do:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Entity">
    <xsl:copy>
        <xsl:for-each-group select="Shipment" group-by="concat(shipFromLocation, '|', shipToLocation)">
            <Shipment>
                <xsl:copy-of select="shipFromLocation, shipToLocation"/>
                <xsl:copy-of select="current-group()/container"/>
            </Shipment>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Related