Home > Net >  XSLT Sorting of Loop is not working
XSLT Sorting of Loop is not working

Time:11-25

My XSLT sort of nodes is not working, please check what's wrong with my xsl. I can only use xslt 1.0.

input XML file

<?xml version="1.0" encoding="UTF-8"?>
<ST_Loop>
    <S5_Loop>
        <S5>
            <S501>1</S501>
            <S502>LD</S502>
        </S5>
        <G62>
            <G6202>20210929</G6202>
            <G6204>010000</G6204>
        </G62>
        <N1_Loop>
            <N1>
                <N101>Milford</N101>
            </N1>
            <N3>
                <N301>49 Sumner St</N301>
            </N3>
            <N4>
                <N401>Worcester</N401>
                <N403>01757-1656</N403>
                <N404>US</N404>
            </N4>
        </N1_Loop>
        <OID_Loop/>
    </S5_Loop>
    <S5_Loop>
        <S5>
            <S501>3</S501>
            <S502>LD</S502>
        </S5>
        <G62/>
        <N1_Loop>
            <N1>
                <N101>MOISON ACE HDWE OF BEDFORD LLC</N101>
            </N1>
            <N3>
                <N301>297 Great Rd</N301>
            </N3>
            <N4>
                <N401>Middlesex</N401>
                <N403>01730-2802</N403>
                <N404>US</N404>
            </N4>
        </N1_Loop>
        <OID_Loop/>
    </S5_Loop>
    <S5_Loop>
        <S5>
            <S501>5</S501>
            <S502>LD</S502>
        </S5>
        <G62/>
        <N1_Loop>
            <N1>
                <N101>WATERS &amp; BROWN, INC.        02</N101>
            </N1>
            <N3>
                <N301>13 Elliott St</N301>
            </N3>
            <N4>
                <N401>Essex</N401>
                <N403>01915-3308</N403>
                <N404>US</N404>
            </N4>
        </N1_Loop>
        <OID_Loop/>
    </S5_Loop>
    <S5_Loop>
        <S5>
            <S501>2</S501>
            <S502>UL</S502>
        </S5>
        <G62>
            <G6202>20210929</G6202>
            <G6204>040000</G6204>
        </G62>
        <LAD/>
        <N1_Loop>
            <N1>
                <N101>ST</N101>
                <N102>MOISON ACE HDWE OF BEDFORD LLC</N102>
            </N1>
            <N3>
                <N301>297 Great Rd</N301>
            </N3>
            <N4>
                <N401>Bedford</N401>
                <N402>MA</N402>
                <N403>01730-2802</N403>
                <N404>US</N404>
            </N4>
        </N1_Loop>
    </S5_Loop>
    <S5_Loop>
        <S5>
            <S501>4</S501>
            <S502>UL</S502>
        </S5>
        <G62>
            <G6202>20210929</G6202>
            <G6204>040000</G6204>
        </G62>
        <LAD/>
        <N1_Loop>
            <N1>
                <N101>ST</N101>
                <N102>WATERS &amp; BROWN, INC.        02</N102>
            </N1>
            <N3>
                <N301>13 Elliott St</N301>
            </N3>
            <N4>
                <N401>Beverly</N401>
                <N402>MA</N402>
                <N403>01915-3308</N403>
                <N404>US</N404>
            </N4>
        </N1_Loop>
    </S5_Loop>
    <S5_Loop>
        <S5>
            <S501>6</S501>
            <S502>UL</S502>
        </S5>
        <G62>
            <G6202>20210929</G6202>
            <G6204>040000</G6204>
        </G62>
        <LAD/>
        <N1_Loop>
            <N1>
                <N101>ST</N101>
                <N102>HOME DECOR GROUP            03</N102>
            </N1>
            <N3>
                <N301>450 Paradise Rd</N301>
            </N3>
            <N4>
                <N401>Swampscott</N401>
                <N402>MA</N402>
                <N403>01907-1300</N403>
                <N404>US</N404>
            </N4>
        </N1_Loop>
    </S5_Loop>
</ST_Loop>

XSLT File:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
 
 <xsl:template match="/ST_Loop/S5_Loop">
    <xsl:copy>
      <xsl:apply-templates>
         <xsl:sort data-type="number" select="/ST_Loop/S5_Loop/S5/S501"/>
         </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>           

Output I wanted (Sort S5_Loop based on S501 value)

<?xml version="1.0" encoding="UTF-8"?>
<ST_Loop>
   <S5_Loop>
      <S5>
         <S501>1</S501>
         <S502>LD</S502>
      </S5>
      <G62>
         <G6202>20210929</G6202>
         <G6204>010000</G6204>
      </G62>
      <N1_Loop>
         <N1>
            <N101>Milford</N101>
         </N1>
         <N3>
            <N301>49 Sumner St</N301>
         </N3>
         <N4>
            <N401>Worcester</N401>
            <N403>01757-1656</N403>
            <N404>US</N404>
         </N4>
      </N1_Loop>
      <OID_Loop />
   </S5_Loop>
   <S5_Loop>
      <S5>
         <S501>2</S501>
         <S502>UL</S502>
      </S5>
      <G62>
         <G6202>20210929</G6202>
         <G6204>040000</G6204>
      </G62>
      <LAD />
      <N1_Loop>
         <N1>
            <N101>ST</N101>
            <N102>MOISON ACE HDWE OF BEDFORD LLC</N102>
         </N1>
         <N3>
            <N301>297 Great Rd</N301>
         </N3>
         <N4>
            <N401>Bedford</N401>
            <N402>MA</N402>
            <N403>01730-2802</N403>
            <N404>US</N404>
         </N4>
      </N1_Loop>
   </S5_Loop>
   <S5_Loop>
      <S5>
         <S501>3</S501>
         <S502>LD</S502>
      </S5>
      <G62 />
      <N1_Loop>
         <N1>
            <N101>MOISON ACE HDWE OF BEDFORD LLC</N101>
         </N1>
         <N3>
            <N301>297 Great Rd</N301>
         </N3>
         <N4>
            <N401>Middlesex</N401>
            <N403>01730-2802</N403>
            <N404>US</N404>
         </N4>
      </N1_Loop>
      <OID_Loop />
   </S5_Loop>
   <S5_Loop>
      <S5>
         <S501>4</S501>
         <S502>UL</S502>
      </S5>
      <G62>
         <G6202>20210929</G6202>
         <G6204>040000</G6204>
      </G62>
      <LAD />
      <N1_Loop>
         <N1>
            <N101>ST</N101>
            <N102>WATERS &amp; BROWN, INC.        02</N102>
         </N1>
         <N3>
            <N301>13 Elliott St</N301>
         </N3>
         <N4>
            <N401>Beverly</N401>
            <N402>MA</N402>
            <N403>01915-3308</N403>
            <N404>US</N404>
         </N4>
      </N1_Loop>
   </S5_Loop>
   <S5_Loop>
      <S5>
         <S501>5</S501>
         <S502>LD</S502>
      </S5>
      <G62 />
      <N1_Loop>
         <N1>
            <N101>WATERS &amp; BROWN, INC.        02</N101>
         </N1>
         <N3>
            <N301>13 Elliott St</N301>
         </N3>
         <N4>
            <N401>Essex</N401>
            <N403>01915-3308</N403>
            <N404>US</N404>
         </N4>
      </N1_Loop>
      <OID_Loop />
   </S5_Loop>
   <S5_Loop>
      <S5>
         <S501>6</S501>
         <S502>UL</S502>
      </S5>
      <G62>
         <G6202>20210929</G6202>
         <G6204>040000</G6204>
      </G62>
      <LAD />
      <N1_Loop>
         <N1>
            <N101>ST</N101>
            <N102>HOME DECOR GROUP            03</N102>
         </N1>
         <N3>
            <N301>450 Paradise Rd</N301>
         </N3>
         <N4>
            <N401>Swampscott</N401>
            <N402>MA</N402>
            <N403>01907-1300</N403>
            <N404>US</N404>
         </N4>
      </N1_Loop>
   </S5_Loop>
</ST_Loop>

CodePudding user response:

Instead of:

 <xsl:template match="/ST_Loop/S5_Loop">
    <xsl:copy>
      <xsl:apply-templates>
         <xsl:sort data-type="number" select="/ST_Loop/S5_Loop/S5/S501"/>
         </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

do:

<xsl:template match="/ST_Loop">
    <xsl:copy>
        <xsl:apply-templates>
            <xsl:sort data-type="number" select="S5/S501"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

i.e. match the parent element of the nodes you want to sort, and apply templates to the children, using a relative path to the sort key.

CodePudding user response:

Instead of

 <xsl:template match="/ST_Loop/S5_Loop">
    <xsl:copy>
      <xsl:apply-templates>
         <xsl:sort data-type="number" select="/ST_Loop/S5_Loop/S5/S501"/>
         </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

use

 <xsl:template match="/ST_Loop/">
    <xsl:copy>
      <xsl:apply-templates select="S5_Loop">
         <xsl:sort data-type="number" select="S5/S501"/>
         </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  • Related