应用于数字时的xslt排序

时间:2013-05-23 11:12:46

标签: xslt xslt-1.0

我创建了xslt,用于在xml下面进行排序。输出似乎没有基于AccountNumber进行排序。不确定我的错误是什么。

<TXLife xmlns="http://ACORD.org/Standards/Life/2">
    <TXLifeRequest>
        <FundCode>LTRT00</FundCode>
        <AccountDescription>CWA – +U</AccountDescription>
        <CurrencyTypeCode>840</CurrencyTypeCode>
        <TransExeDate>2013-04-20</TransExeDate>
        <AccountNumber>34142</AccountNumber>
        <PaymentAmt>300.000000000</PaymentAmt>
        <ReversalInd>0</ReversalInd>
    </TXLifeRequest>
    <TXLifeRequest>
        <FundCode>LTRW00</FundCode>
        <AccountDescription>CWA – +U</AccountDescription>
        <CurrencyTypeCode>124</CurrencyTypeCode>
        <TransExeDate>2013-04-20</TransExeDate>
        <AccountNumber>14142</AccountNumber>
        <PaymentAmt>250.000000000</PaymentAmt>
        <ReversalInd>0</ReversalInd>
    </TXLifeRequest>   
</TXLife>

XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns="http://ACORD.org/Standards/Life/2">
    <xsl:output indent="yes"/>
     <xsl:strip-space elements="*"/>


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


    <xsl:template match="@ns:*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="." />
        </xsl:attribute>
    </xsl:template>


    <xsl:template match="/">

           <xsl:for-each select="/ns:TXLife/ns:TXLifeRequest">

               <xsl:sort select="ns:AccountNumber" order="ascending" data-type="number"/>

           </xsl:for-each>
            <xsl:apply-templates select="*"></xsl:apply-templates> 

    </xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="utf-8"?>
<TXLife xmlns="http://ACORD.org/Standards/Life/2">
   <TXLifeRequest>
      <FundCode>LTRT00</FundCode>
      <AccountDescription>CWA – +U</AccountDescription>
      <CurrencyTypeCode>840</CurrencyTypeCode>
      <TransExeDate>2013-04-20</TransExeDate>
      <AccountNumber>34142</AccountNumber>
      <PaymentAmt>300.000000000</PaymentAmt>
      <ReversalInd>0</ReversalInd>
   </TXLifeRequest>
   <TXLifeRequest>
      <FundCode>LTRW00</FundCode>
      <AccountDescription>CWA – +U</AccountDescription>
      <CurrencyTypeCode>124</CurrencyTypeCode>
      <TransExeDate>2013-04-20</TransExeDate>
      <AccountNumber>14142</AccountNumber>
      <PaymentAmt>250.000000000</PaymentAmt>
      <ReversalInd>0</ReversalInd>
   </TXLifeRequest>
</TXLife>

不确定我在xslt中缺少什么。我尝试使用data-type =“text”但输出仍未排序。

1 个答案:

答案 0 :(得分:1)

您没有在for-each循环中输出任何内容 将apply-templates移至for-each。如下所示:

<xsl:template match="/">
    <xsl:for-each select="/ns:TXLife/ns:TXLifeRequest">
        <xsl:sort select="ns:AccountNumber" order="ascending" data-type="number"/>
        <xsl:copy>
            <xsl:apply-templates select="*"></xsl:apply-templates>      
        </xsl:copy>
    </xsl:for-each>
</xsl:template>