Home > OS >  XSLT Sorting of Loops is not workin
XSLT Sorting of Loops is not workin

Time:11-29

My XSLT sorting of nodes is not working, please check what's wrong with my xsl. I can only use xslt 1.0 for the solution. Here is the input and output I needed, where to put the Sort Information. Is there a way to sort the loops with ascending order.

Here is the 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></N101>
            </N1>
            <N3>
                <N301></N301>
            </N3>
            <N4>
                <N401></N401>
                <N403>-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></N101>
            </N1>
            <N3>
                <N301></N301>
            </N3>
            <N4>
                <N401></N401>
                <N403>-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></N101>
            </N1>
            <N3>
                <N301></N301>
            </N3>
            <N4>
                <N401>Essex</N401>
                <N403>-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></N102>
            </N1>
            <N3>
                <N301></N301>
            </N3>
            <N4>
                <N401></N401>
                <N402></N402>
                <N403>-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></N102>
            </N1>
            <N3>
                <N301></N301>
            </N3>
            <N4>
                <N401></N401>
                <N402></N402>
                <N403>-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></N102>
            </N1>
            <N3>
                <N301></N301>
            </N3>
            <N4>
                <N401></N401>
                <N402></N402>
                <N403>-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></N101>
         </N1>
         <N3>
            <N301></N301>
         </N3>
         <N4>
            <N401></N401>
            <N403>-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></N102>
         </N1>
         <N3>
            <N301></N301>
         </N3>
         <N4>
            <N401></N401>
            <N402></N402>
            <N403>-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></N101>
         </N1>
         <N3>
            <N301></N301>
         </N3>
         <N4>
            <N401></N401>
            <N403>-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></N102>
         </N1>
         <N3>
            <N301></N301>
         </N3>
         <N4>
            <N401></N401>
            <N402></N402>
            <N403>-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></N101>
         </N1>
         <N3>
            <N301></N301>
         </N3>
         <N4>
            <N401></N401>
            <N403>-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></N102>
         </N1>
         <N3>
            <N301></N301>
         </N3>
         <N4>
            <N401></N401>
            <N402>MA</N402>
            <N403>-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