XSLT sorting help / not working for me
Posted: Wed Nov 10, 2021 6:59 pm
Hi All,
So, I have an XML like below -
I need to sort all the ExtnLocationInfo elements inside ShipmentLine tag in ascending order of attribute value FloorLocation.
So Output should be like below
I am trying something like below :
But it is not sorting for me. Please let me know what I am doing wrong. I am really new to this.
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>
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>
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>