根据字段值按升序对记录进行排序

Sorting the records based on the field value in ascending order

我有下面的详细记录,里面的详细记录有多个段在重复。根据客户编号Detail需要升序排序。下面XML有2条明细记录,每条明细记录包含多个段。第一个详细记录是相关的 customerno 120,第二个是 customerno 99。输出应按升序排列。第一个应该是 99,第二个应该是 120。 请帮助我满足以下要求。

输入xml

<?xml version="1.0" encoding="UTF-8"?>
 <ns0:Invoice
xmlns:ns0="urn:file.com:integration:api">
<Header_00>
    <Type>00</Type>
    <Filler/>
    <Name>EDG</Name>
</Header_00>
<Detail>
    <ID>
        <Type>1</Type>
        <Customerno>120</Customerno>
        <BillingDate>20200201</BillingDate>
        <FromDate>00000000</FromDate>
    </ID>
    <Totals>
        <Type>9</Type>
        <Customerno>120</Customerno>
        <Due>135</Due>
    </Totals>
    <Totals_1>
        <Type>8</Type>
        <Customerno>120</Customerno>
        <Unit>E</Unit>
        <Due>00001</Due>
    </Totals_1>
    <Totals_1>
        <Type>8</Type>
        <Customerno>120</Customerno>
        <DistributionGroup/>
        <Unit>4</Unit>
        <Due>80</Due>
    </Totals_1>
</Detail>
<Detail>
    <ID>
        <Type>1</Type>
        <Customerno>99</Customerno>
        <BillingDate>20210201</BillingDate>
        <FromDate>00000000</FromDate>
    </ID>
    <Totals>
        <Type>9</Type>
        <Customerno>99</Customerno>
        <Due>789</Due>
    </Totals>
    <Totals_1>
        <Type>8</Type>
        <Customerno>99</Customerno>
        <Unit>4</Unit>
        <Due>0071</Due>
    </Totals_1>
</Detail>
<Trailer>
    <Type>90</Type>
    <Number>0000002</Number>
    <NumberOfRecords>2</NumberOfRecords>
</Trailer>
</ns0:Invoice>

输出xml

<?xml version="1.0" encoding="UTF-8"?>
 <ns0:Invoice xmlns:ns0="urn:file.com:integration:api">
  <Header_00>
  <Type>00</Type>
  <Filler/>
  <Name>EDG</Name>    
  </Header_00>
    <Detail>
       <ID>
     <Type>1</Type>
     <Customerno>99</Customerno>
     
     <BillingDate>20210201</BillingDate>
     <FromDate>00000000</FromDate>
    
  </ID>
  <Totals>
     <Type>9</Type>
     <Customerno>99</Customerno>
     <Due>789</Due>
     </Totals>
     <Totals_1>
     <Type>8</Type>
     <Customerno>99</Customerno>
      <Unit>4</Unit>
    <Due>0071</Due>
     </Totals_1>
    </Detail>
    <Detail>
     <ID>
     <Type>1</Type>
     <Customerno>120</Customerno>
     <BillingDate>20200201</BillingDate>
     <FromDate>00000000</FromDate>
     </ID>
     <Totals>
     <Type>9</Type>
     <Customerno>120</Customerno>
     <Due>135</Due>
     </Totals>
      <Totals_1>
     <Type>8</Type>
     <Customerno>120</Customerno>
     
     <Unit>E</Unit>
     <Due>00001</Due>
     >
  </Totals_1>
  <Totals_1>
     <Type>8</Type>
     <Customerno>120</Customerno>
     <DistributionGroup/>
     <Unit>4</Unit>
     <Due>80</Due>
      </Totals_1>
     </Detail>
    <Trailer>
    <Type>90</Type>
  <Number>0000002</Number>
  <NumberOfRecords>2</NumberOfRecords>
   </Trailer>
   </ns0:Invoice>

XSLT 代码

<?xml version="1.0" encoding="utf-8"?>

 <xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
 <xsl:copy>
 <xsl:apply-templates select="@*|node()">
 <xsl:sort select="Customerno" order ="ascending"/>
  </xsl:apply-templates>
 </xsl:copy>

 </xsl:template>

 </xsl:stylesheet>

您不能使用:

<xsl:sort select="Customerno" order ="ascending"/>

因为您想对 Detail 个节点进行排序 - 而 Customerno 不是 Detail 的子节点。此外,如果您只想对 Detail 个节点进行排序,则只对 Detail 个节点进行排序:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="urn:file.com:integration:api">
<xsl:output method="xml" indent="yes"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/ns0:Invoice">
    <xsl:copy>
        <xsl:apply-templates select="Header_00"/>
        <xsl:apply-templates select="Detail">
            <xsl:sort select="ID/Customerno" data-type="number"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="Trailer"/>
    </xsl:copy>
</xsl:template>

 </xsl:stylesheet>