XSLT sorting help / not working for me

Here should go questions about transforming XML with XSLT and FOP.
victor_alpha
Posts: 1
Joined: Wed Nov 10, 2021 6:54 pm

XSLT sorting help / not working for me

Post by victor_alpha »

Hi All,

So, I have an XML like below -

Code: Select all

<Shipments>
    <Shipment CarrierServiceCode="Ground" DeliveryMethod="SHP" ExpectedShipmentDate="2021-10-18T16:43:48+00:00"   StatusDate="2021-10-18T16:43:57+00:00">
        <Extn ExtnSLAExpectedShipDate="2021-10-19T16:43:48+00:00"/>
        <ShipmentLines TotalNumberOfRecords="5">
            <ShipmentLine >
                <Extn>
                    <ExtnLocationInfoList>
                        <ExtnLocationInfo FloorLocation="15NDAR" FloorLocationDesc="Needle Arts"/>
                    </ExtnLocationInfoList>
                </Extn>
            </ShipmentLine>
            <ShipmentLine >
                <Extn>
                    <ExtnLocationInfoList>
                        <ExtnLocationInfo FloorLocation="10SWCN" FloorLocationDesc="Sewing Construction"/>
                    </ExtnLocationInfoList>
                </Extn>
            </ShipmentLine>
            <ShipmentLine >
                <Extn>
                    <ExtnLocationInfoList>
                        <ExtnLocationInfo FloorLocation="15NDAR" FloorLocationDesc="Needle Arts"/>
                    </ExtnLocationInfoList>
                </Extn>
            </ShipmentLine>
            <ShipmentLine >
                <Extn>
                    <ExtnLocationInfoList>
                        <ExtnLocationInfo FloorLocation="15NDAR" FloorLocationDesc="Needle Arts"/>
                    </ExtnLocationInfoList>
                </Extn>
            </ShipmentLine>
        </ShipmentLines>
    </Shipment>
    </Shipments>
I need to sort all the ExtnLocationInfo elements inside ShipmentLine tag in ascending order of attribute value FloorLocation.

So Output should be like below

Code: Select all

<Shipments>
    <Shipment CarrierServiceCode="Ground" DeliveryMethod="SHP" ExpectedShipmentDate="2021-10-18T16:43:48+00:00" StatusDate="2021-10-18T16:43:57+00:00">
        <Extn ExtnSLAExpectedShipDate="2021-10-19T16:43:48+00:00"/>
        <ShipmentLines TotalNumberOfRecords="5">
            <ShipmentLine>
                <Extn>
                    <ExtnLocationInfoList>
                        <ExtnLocationInfo FloorLocation="10SWCN" FloorLocationDesc="Sewing Construction"/>
                    </ExtnLocationInfoList>
                </Extn>
            </ShipmentLine>
            <ShipmentLine>
                <Extn>
                    <ExtnLocationInfoList>
                        <ExtnLocationInfo FloorLocation="15NDAR" FloorLocationDesc="Needle Arts"/>
                    </ExtnLocationInfoList>
                </Extn>
            </ShipmentLine>
            <ShipmentLine>
                <Extn>
                    <ExtnLocationInfoList>
                        <ExtnLocationInfo FloorLocation="15NDAR" FloorLocationDesc="Needle Arts"/>
                    </ExtnLocationInfoList>
                </Extn>
            </ShipmentLine>
            <ShipmentLine>
                <Extn>
                    <ExtnLocationInfoList>
                        <ExtnLocationInfo FloorLocation="15NDAR" FloorLocationDesc="Needle Arts"/>
                    </ExtnLocationInfoList>
                </Extn>
            </ShipmentLine>
        </ShipmentLines>
    </Shipment>
</Shipments>
I am trying something like below :


Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/Shipments/Shipment/*">
        <xsl:copy>
            <xsl:apply-templates>
                <xsl:sort data-type="text" select="/Shipments/Shipment/ShipmentLines/Extn/ExtnLocationInfoList/ExtnLocationInfo/@FloorLocation"
                    order="ascending" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
But it is not sorting for me. Please let me know what I am doing wrong. I am really new to this.