Click here to Skip to main content
15,868,016 members
Home / Discussions / XML / XSL
   

XML / XSL

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder16-Jul-09 3:10
cofounderChris Maunder16-Jul-09 3:10 
PinnedHow to get an answer to your question PinPopular
Chris Maunder16-Jul-09 3:06
cofounderChris Maunder16-Jul-09 3:06 
QuestionHow to create XSD classes for Jackson. Pin
Syed Mubashshir Hasan17-Oct-22 0:05
Syed Mubashshir Hasan17-Oct-22 0:05 
QuestionGrouping XML key value using XSLT - Need help Pin
Manju Kaushik22-Jun-22 0:25
Manju Kaushik22-Jun-22 0:25 
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: - 
<pre lang="XML"><?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
<?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,


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


QuestionNeed help with XML/XSL Pin
Duke Jason2-Dec-21 18:42
Duke Jason2-Dec-21 18:42 
AnswerRe: Need help with XML/XSL Pin
thatraja12-Jan-22 5:24
professionalthatraja12-Jan-22 5:24 
QuestionHow to suppress empty tag using xsl Pin
cartriders16-Dec-20 12:47
cartriders16-Dec-20 12:47 
AnswerRe: How to suppress empty tag using xsl Pin
thatraja5-Jan-21 3:32
professionalthatraja5-Jan-21 3:32 
QuestionHow to add Key and MergeField dynamically into WordProcessing OpenXML document C# program Pin
Ganapatisb10-Sep-20 22:24
Ganapatisb10-Sep-20 22:24 
QuestionComparing two elements in XSLT Pin
Member 148262328-May-20 9:34
Member 148262328-May-20 9:34 
AnswerRe: Comparing two elements in XSLT Pin
Richard Deeming10-May-20 21:58
mveRichard Deeming10-May-20 21:58 
QuestionI don't see WebBrowser?? (webbrowser error) (wpf) Pin
Member 1477397215-Mar-20 21:58
Member 1477397215-Mar-20 21:58 
AnswerRe: I don't see WebBrowser?? (webbrowser error) (wpf) Pin
Richard MacCutchan15-Mar-20 22:56
mveRichard MacCutchan15-Mar-20 22:56 
QuestionSchematron errors with XML Spy update Pin
Member 1459998621-Sep-19 16:33
Member 1459998621-Sep-19 16:33 
AnswerRe: Schematron errors with XML Spy update Pin
Richard Deeming22-Oct-19 0:49
mveRichard Deeming22-Oct-19 0:49 
QuestionXML file read images and display them at random Pin
eatupyourgreens12-Sep-19 9:33
eatupyourgreens12-Sep-19 9:33 
AnswerRe: XML file read images and display them at random Pin
Richard MacCutchan12-Sep-19 11:20
mveRichard MacCutchan12-Sep-19 11:20 
GeneralRe: XML file read images and display them at random Pin
eatupyourgreens12-Sep-19 11:27
eatupyourgreens12-Sep-19 11:27 
GeneralRe: XML file read images and display them at random Pin
Richard MacCutchan12-Sep-19 11:33
mveRichard MacCutchan12-Sep-19 11:33 
QuestionQuery XML API and display results Pin
hm928-Aug-19 4:37
hm928-Aug-19 4:37 
AnswerRe: Query XML API and display results Pin
Richard MacCutchan28-Aug-19 5:57
mveRichard MacCutchan28-Aug-19 5:57 
QuestionDate conversion failure in xsl file (Chinese characters showing in date field) Pin
AkashBabu25-Feb-19 2:34
AkashBabu25-Feb-19 2:34 
AnswerRe: Date conversion failure in xsl file (Chinese characters showing in date field) Pin
Richard Deeming25-Feb-19 8:51
mveRichard Deeming25-Feb-19 8:51 
QuestionDeserializing of List Array appends instead of overwrite Pin
Wild Thing13-Jul-18 0:53
Wild Thing13-Jul-18 0:53 
QuestionHow do I change this code to store new values to data list Pin
DropItSweet9-Jun-18 23:26
DropItSweet9-Jun-18 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.