×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Grouping XML key value using XSLT

Grouping XML key value using XSLT

Grouping XML key value using XSLT

(OP)
Hi All,

There is a query for which i am seeing assistance, on how to group the XML by keyElements using XSLT. Below is the sample XML code: -
<?xml version = "1.0" encoding = "UTF-8"?>
<CISDocument>
<Aheader>
<OperationName>APINAME</OperationName>
</Aheader>
<ResponseHeader>
<CompletedSuccessfully>true</CompletedSuccessfully>
</ResponseHeader>
<Page>
<StartAtRow>0</StartAtRow>
<MaxRows>5</MaxRows>
<TotalRowCount>1</TotalRowCount>
</Page>
<Entity>
<Load>
<LID>A</LID>
<ccode>B</ccode>
<servicecode>C</servicecode>
<flocationcode>D</flocationcode>
<llocationcode>E</llocationcode>
<BaseShipmentModule>
<ShipUnitModule>
<Shnum>1234567</Shnum>
<Sfromlocation>01234</Sfromlocation>
<Stolocation>04567</Stolocation>
<Container>
<SContainerID>1</SContainerID>
<Quantity>1</Quantity>
<ContainerVinformation>
<Volume>1</Volume>
</ContainerVinformation>
<ItemNumber>AA</ItemNumber>
<WFClass>
<Fweigth>222</Fweigth>
<FClassCode>10</FClassCode>
</WFClass>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>BBB</ReferenceNumberTypeCode>
<ReferenceNumber>CCC</ReferenceNumber>
</ReferenceNumberStructure>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>DDD</ReferenceNumberTypeCode>
<ReferenceNumber>1233</ReferenceNumber>
</ReferenceNumberStructure>
</Container>
</ShipUnitModule>
</BaseShipmentModule>
<BaseShipmentModule>
<ShipUnitModule>
<Shnum>1234568</Shnum>
<Sfromlocation>01234</Sfromlocation>
<Stolocation>04567</Stolocation>
<Container>
<SContainerID>1</SContainerID>
<Quantity>1</Quantity>
<ContainerVinformation>
<Volume>1</Volume>
</ContainerVinformation>
<ItemNumber>AA</ItemNumber>
<WFClass>
<Fweigth>222</Fweigth>
<FClassCode>10</FClassCode>
</WFClass>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>BBB</ReferenceNumberTypeCode>
<ReferenceNumber>CCC</ReferenceNumber>
</ReferenceNumberStructure>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>DDD</ReferenceNumberTypeCode>
<ReferenceNumber>1233</ReferenceNumber>
</ReferenceNumberStructure>
</Container>
</ShipUnitModule>
</BaseShipmentModule>
<BaseShipmentModule>
<ShipUnitModule>
<Shnum>1234569</Shnum>
<Sfromlocation>11234</Sfromlocation>
<Stolocation>14567</Stolocation>
<Container>
<SContainerID>1</SContainerID>
<Quantity>1</Quantity>
<ContainerVinformation>
<Volume>1</Volume>
</ContainerVinformation>
<ItemNumber>AA</ItemNumber>
<WFClass>
<Fweigth>222</Fweigth>
<FClassCode>10</FClassCode>
</WFClass>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>BBB</ReferenceNumberTypeCode>
<ReferenceNumber>CCC</ReferenceNumber>
</ReferenceNumberStructure>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>DDD</ReferenceNumberTypeCode>
<ReferenceNumber>1233</ReferenceNumber>
</ReferenceNumberStructure>
</Container>
</ShipUnitModule>
</BaseShipmentModule>
<BaseShipmentModule>
<ShipUnitModule>
<Shnum>1234570</Shnum>
<Sfromlocation>11234</Sfromlocation>
<Stolocation>14567</Stolocation>
<Container>
<SContainerID>1</SContainerID>
<Quantity>1</Quantity>
<ContainerVinformation>
<Volume>1</Volume>
</ContainerVinformation>
<ItemNumber>AA</ItemNumber>
<WFClass>
<Fweigth>222</Fweigth>
<FClassCode>10</FClassCode>
</WFClass>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>BBB</ReferenceNumberTypeCode>
<ReferenceNumber>CCC</ReferenceNumber>
</ReferenceNumberStructure>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>DDD</ReferenceNumberTypeCode>
<ReferenceNumber>1233</ReferenceNumber>
</ReferenceNumberStructure>
</Container>
</ShipUnitModule>
</BaseShipmentModule>
<S1>
<SID>1613243
</SSID>
<SCount>3</SCount>
<CDStop>0</CDStop>
<AATime>2022-06-17T08:00:00</AATime>
<SLCode>06V0018963</SLCode>
</S1>
<S1>
<SSID>1613244</SSID>
<SCount>0</SCount>
<CDStop>16</CDStop>
<AATime>2022-06-17T19:45:00</AATime>
<SLCode>06C0124A</SLCode>
</S1>
<S1>
<SSID>1613245</SSID>
<SCount>4</SCount>
<CDStop>0</CDStop>
<AATime>2022-06-17T09:00:00</AATime>
<SLCode>06V0029699</SLCode>
</S1>
</Stop>
</Load>
</Entity>

As we can see in the above XML example, there are multiple <BaseShipmentModule> tag and within each <BaseShipmentModule> we have a tag called: <Sfromlocation> and <Stolocation> which stands for location code, from and to location.



Now if we observe a little bit, we have a total of 4 <BaseShipmentModule> tag and the value for <Sfromlocation> and <Stolocation> in same in the 1st and 2nd <BaseShipmentModule> tag. Where as the value for <Sfromlocation> and <Stolocation> is same in the 3rd and 4th <BaseShipmentModule> tag. Looking for to group the XML based on the <Sfromlocation> and <Stolocation>.



Expected output is shown below: -

<?xml version = "1.0" encoding = "UTF-8"?>
<CISDocument>
<Aheader>
<OperationName>APINAME</OperationName>
</Aheader>
<ResponseHeader>
<CompletedSuccessfully>true</CompletedSuccessfully>
</ResponseHeader>
<Page>
<StartAtRow>0</StartAtRow>
<MaxRows>5</MaxRows>
<TotalRowCount>1</TotalRowCount>
</Page>
<Entity>
<Load>
<LID>A</LID>
<ccode>B</ccode>
<servicecode>C</servicecode>
<flocationcode>D</flocationcode>
<llocationcode>E</llocationcode>
<BaseShipmentModule>
<ShipUnitModule>
<Shnum>1234567</Shnum>
<Shnum>1234568</Shnum>
<Sfromlocation>01234</Sfromlocation>
<Stolocation>04567</Stolocation>
<Container>
<SContainerID>1</SContainerID>
<Quantity>1</Quantity>
<ContainerVinformation>
<Volume>1</Volume>
</ContainerVinformation>
<ItemNumber>AA</ItemNumber>
<WFClass>
<Fweigth>222</Fweigth>
<FClassCode>10</FClassCode>
</WFClass>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>BBB</ReferenceNumberTypeCode>
<ReferenceNumber>CCC</ReferenceNumber>
</ReferenceNumberStructure>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>DDD</ReferenceNumberTypeCode>
<ReferenceNumber>1233</ReferenceNumber>
</ReferenceNumberStructure>
</Container>
<Container>
<SContainerID>1</SContainerID>
<Quantity>1</Quantity>
<ContainerVinformation>
<Volume>1</Volume>
</ContainerVinformation>
<ItemNumber>AA</ItemNumber>
<WFClass>
<Fweigth>222</Fweigth>
<FClassCode>10</FClassCode>
</WFClass>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>BBB</ReferenceNumberTypeCode>
<ReferenceNumber>CCC</ReferenceNumber>
</ReferenceNumberStructure>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>DDD</ReferenceNumberTypeCode>
<ReferenceNumber>1233</ReferenceNumber>
</ReferenceNumberStructure>
</Container>
</ShipUnitModule>
</BaseShipmentModule>
<BaseShipmentModule>
<ShipUnitModule>
<Shnum>1234569</Shnum>
<Shnum>1234570</Shnum>
<Sfromlocation>11234</Sfromlocation>
<Stolocation>14567</Stolocation>
<Container>
<SContainerID>1</SContainerID>
<Quantity>1</Quantity>
<ContainerVinformation>
<Volume>1</Volume>
</ContainerVinformation>
<ItemNumber>AA</ItemNumber>
<WFClass>
<Fweigth>222</Fweigth>
<FClassCode>10</FClassCode>
</WFClass>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>BBB</ReferenceNumberTypeCode>
<ReferenceNumber>CCC</ReferenceNumber>
</ReferenceNumberStructure>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>DDD</ReferenceNumberTypeCode>
<ReferenceNumber>1233</ReferenceNumber>
</ReferenceNumberStructure>
</Container>
<Container>
<SContainerID>1</SContainerID>
<Quantity>1</Quantity>
<ContainerVinformation>
<Volume>1</Volume>
</ContainerVinformation>
<ItemNumber>AA</ItemNumber>
<WFClass>
<Fweigth>222</Fweigth>
<FClassCode>10</FClassCode>
</WFClass>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>BBB</ReferenceNumberTypeCode>
<ReferenceNumber>CCC</ReferenceNumber>
</ReferenceNumberStructure>
<ReferenceNumberStructure>
<ReferenceNumberTypeCode>DDD</ReferenceNumberTypeCode>
<ReferenceNumber>1233</ReferenceNumber>
</ReferenceNumberStructure>
</Container>
</ShipUnitModule>
</BaseShipmentModule>
<S1>
<SID>1613243
</SSID>
<SCount>3</SCount>
<CDStop>0</CDStop>
<AATime>2022-06-17T08:00:00</AATime>
<SLCode>06V0018963</SLCode>
</S1>
<S1>
<SSID>1613244</SSID>
<SCount>0</SCount>
<CDStop>16</CDStop>
<AATime>2022-06-17T19:45:00</AATime>
<SLCode>06C0124A</SLCode>
</S1>
<S1>
<SSID>1613245</SSID>
<SCount>4</SCount>
<CDStop>0</CDStop>
<AATime>2022-06-17T09:00:00</AATime>
<SLCode>06V0029699</SLCode>
</S1>
</Stop>
</Load>
</Entity>


As we can see in the above result, we have combined the <ShipUnitModule> based on the <Sfromlocation> and <Stolocation> tag. And he tag <Container> has been grouped and the rest of the tag <S1> is unaffected by it. We did get help from one of the Engineers (Michael), with the below XSLT,


<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>


But, it is not able to help the overall output, please do help us here. Its quite important. Any suggestion will matter a lot.

RE: Grouping XML key value using XSLT

198709km,

If I understood correctly your requirements, this will put you closer.

CODE --> XSLT

<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="/">
        <fragment><xsl:apply-templates select="//Entity"/></fragment>
    </xsl:template>
    <xsl:template match="Entity">
        <Entity>
            <Load>
                <xsl:apply-templates select="Load/BaseShipmentModule[1]" mode="first"/>
                <xsl:for-each-group select="Load/BaseShipmentModule" group-by="concat(ShipUnitModule/Sfromlocation, '|', ShipUnitModule/Stolocation)">
                    <BaseShipmentModule>
                        <ShipUnitModule>
                            <xsl:copy-of select="current-group()/ShipUnitModule/Shnum"/>
                            <xsl:copy-of select="(current-group()/ShipUnitModule/Sfromlocation)[1], (current-group()/ShipUnitModule/Stolocation)[1]"/>
                            <xsl:copy-of select="current-group()/ShipUnitModule/Container"/>
                        </ShipUnitModule>
                     </BaseShipmentModule>
               </xsl:for-each-group>
                <xsl:apply-templates select="Load/BaseShipmentModule[position() = last()]" mode="last"/>
            </Load>
        </Entity>
    </xsl:template>
    <xsl:template match="BaseShipmentModule" mode="first">
        <xsl:copy-of select="preceding-sibling::*"/>
    </xsl:template>
    <xsl:template match="BaseShipmentModule" mode="last">
        <xsl:copy-of select="following-sibling::*"/>
    </xsl:template>
</xsl:stylesheet> 

Note: I only took care of the Entity element. The rest is up to you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close