How to sort the value in an output CSV file with XSLT using one of the columns

I have an XML that I am trying to transform using XSLT. I want the output CSV file to be sorted using the TRANNUM Column in ascending order. See a snippet from the XSLT file I want to modify below. The TRANNUM column has 3 possible values which are 10,11 and 12. I want the rows where the TRANNUM value equals 10 to come first followed by 11 and then 12 in the CSV output file. Below XSLT is working but not sorting the values using the TRANNUM values in ascending order but using the values of tns:Company/tns:Code ('RETGP', 'RTLPM', 'RETRT') to sort the output instead of 10, 11, 12

Below is the extract from my XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org"
    xmlns:wd="http://"
    xmlns:tns="http://"
    xmlns:xtt="urn:"
    exclude-result-prefixes="xs math wd tns xtt" version="3.0">
    
    <xsl:param name="p.source"></xsl:param>
    
    <xsl:output method="text" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="tns:OutboundFile">
        <header1>FinJournals&#xD;&#xA;TRANNUM,DATE,PROPERTY,ACCOUNT,POSTMONTH,BOOKNUM,AMOUNT,REMARK,REF,DESC,SEGMENT2,DISPLAYTYPE,ReverseNextMonth</header1>
        <JournalEntry>
            <xsl:for-each-group select="tns:Journal_Entry/tns:Journal_Line[tns:Ledger_Account != '888888']"
                group-by="string-join((tns:Company/tns:Code,tns:Ledger_Account,description),'+')">
            <xsl:sort select="tns:Company/tns:Code" data-type="text" order="ascending"/>
                <xsl:if test="format-number(sum(current-group()/tns:Ledger_Debit_Amount) - sum(current-group()/tns:Ledger_Credit_Amount),'#0.00') != '0.00'">
                    <JournalLine>
                        <xsl:text>&#xD;&#xA;</xsl:text>
                        <TRANNUM>
                            <xsl:choose>
                                <xsl:when test="tns:Company/tns:Code = 'RETGP'">10</xsl:when>
                                <xsl:when test="tns:Company/tns:Code = 'RTLPM'">11</xsl:when>
                                <xsl:when test="tns:Company/tns:Code = 'RETRT'">12</xsl:when>
                            </xsl:choose>
                        </TRANNUM><xsl:text>,</xsl:text>
                        <DATE><xsl:value-of select="format-date(current-date(),'[M01]/[D01]/[Y0001]')"/></DATE><xsl:text>,</xsl:text>
                        <PROPERTY>
                            <xsl:choose>
                                <xsl:when test="tns:Company/tns:Code = 'RETGP'">22000</xsl:when>
                                <xsl:when test="tns:Company/tns:Code = 'RTLPM'">26000</xsl:when>
                                <xsl:when test="tns:Company/tns:Code = 'RETRT'">23000</xsl:when>
                            </xsl:choose>
                        </PROPERTY><xsl:text>,</xsl:text>


Sample Current Output

TRANNUM,DATE,PROPERTY,ACCOUNT,POSTMONTH

10,01/11/2023,22000,720010,01/13/2023,1

10,01/11/2023,22000,720025,01/13/2023,1

10,01/11/2023,22000,211070,01/13/2023,1

10,01/11/2023,22000,720005,01/13/2023,1

10,01/11/2023,22000,211070,01/13/2023,1

12,01/11/2023,23000,720025,01/13/2023,1

12,01/11/2023,23000,211070,01/13/2023,1

11,01/11/2023,26000,211070,01/13/2023,1

11,01/11/2023,26000,211010,01/13/2023,1

11,01/11/2023,26000,211065,01/13/2023,1

11,01/11/2023,26000,211010,01/13/2023,1

11,01/11/2023,26000,211070,01/13/2023,1

11,01/11/2023,26000,211015,01/13/2023,1

Sample Expected Output

TRANNUM,DATE,PROPERTY,ACCOUNT,POSTMONTH

10,01/11/2023,22000,211070,01/13/2023,1

10,01/11/2023,22000,720025,01/13/2023,1

10,01/11/2023,22000,211070,01/13/2023,1

10,01/11/2023,22000,720010,01/13/2023,1

10,01/11/2023,22000,720025,01/13/2023,1

11,01/11/2023,26000,211070,01/13/2023,1

11,01/11/2023,26000,211010,01/13/2023,1

11,01/11/2023,26000,211065,01/13/2023,1

11,01/11/2023,26000,211010,01/13/2023,1

11,01/11/2023,26000,211015,01/13/2023,1

11,01/11/2023,26000,211070,01/13/2023,1

12,01/11/2023,23000,720005,01/13/2023,1

12,01/11/2023,23000,211070,01/13/2023,1

Upvotes: 0

Views: 95

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

Perhaps add e.g.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org"
    xmlns:wd="http://"
    xmlns:tns="http://"
    xmlns:xtt="urn:"
    exclude-result-prefixes="xs math wd tns xtt" version="3.0">
    
    <xsl:param name="p.source"></xsl:param>
    
    <xsl:output method="text" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="tns:OutboundFile">

    <xsl:param name="code-map" as="map(xs:string, xs:integer)"
      select="map { 'RETGP' : 10, 'RTLPM' : 11, 'RETRT' : 12 }"/>
        <header1>FinJournals&#xD;&#xA;TRANNUM,DATE,PROPERTY,ACCOUNT,POSTMONTH,BOOKNUM,AMOUNT,REMARK,REF,DESC,SEGMENT2,DISPLAYTYPE,ReverseNextMonth</header1>
        <JournalEntry>
            <xsl:for-each-group select="tns:Journal_Entry/tns:Journal_Line[tns:Ledger_Account != '888888']"
                group-by="string-join((tns:Company/tns:Code,tns:Ledger_Account,description),'+')">
                <xsl:sort select="$code-map(tns:Company/tns:Code)"/>
                <xsl:if test="format-number(sum(current-group()/tns:Ledger_Debit_Amount) - sum(current-group()/tns:Ledger_Credit_Amount),'#0.00') != '0.00'">
                    <JournalLine>
                        <xsl:text>&#xD;&#xA;</xsl:text>
                        <TRANNUM>
                            <xsl:choose>
                                <xsl:when test="tns:Company/tns:Code = 'RETGP'">10</xsl:when>
                                <xsl:when test="tns:Company/tns:Code = 'RTLPM'">11</xsl:when>
                                <xsl:when test="tns:Company/tns:Code = 'RETRT'">12</xsl:when>
                            </xsl:choose>
                        </TRANNUM>

Untested as no input sample was provided.

Upvotes: 0

Related Questions