开发者

Sort by Date a XML with XSLT and return a XML

开发者 https://www.devze.com 2023-02-11 00:35 出处:网络
Hello i have this XML and i want to sort all the MESSAGE/DATA/TRANSACTIONS/TRANSACTION With XSLT by \"POSTED-DATE\" and return the sorted XML to work with.

Hello i have this XML and i want to sort all the MESSAGE/DATA/TRANSACTIONS/TRANSACTION With XSLT by "POSTED-DATE" and return the sorted XML to work with.

i have hours reading about XSL and trying to do that but it doesnt work.

thanks in advance

  <DATA RESULT="TRUE">
      <ACCOUNT NUMBER="MyNumber" CURRENCY="MyCurrency" TYPE="MyType" /> 
      <TRANSACTIONS>
          <TRANSACTION DESCRIPTION="DESC 1" CURRENCY="USD" POSTED-DATE="2010/01/31" AMOUNT="240.88" /> 
          <TRANSACTION DESCRIPTION="DESC 3" CURRENCY="US开发者_如何学运维D" POSTED-DATE="2010/05/20" AMOUNT="240.88" /> 
          <TRANSACTION DESCRIPTION="DESC 8" CURRENCY="USD" POSTED-DATE="2008/12/31" AMOUNT="240.88" /> 
          <TRANSACTION DESCRIPTION="DESC 3" CURRENCY="USD" POSTED-DATE="2003/12/31" AMOUNT="240.88" /> 
          <TRANSACTION DESCRIPTION="DESC 99" CURRENCY="USD" POSTED-DATE="2011/01/01" AMOUNT="240.88" /> 
          <TRANSACTION DESCRIPTION="DESC 999" CURRENCY="USD" POSTED-DATE="2011/01/06" AMOUNT="240.88" /> 
      </TRANSACTIONS>
  </DATA>

Thanks in advance


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="TRANSACTIONS">
  <TRANSACTIONS>
   <xsl:apply-templates>
    <xsl:sort select="@POSTED-DATE"/>
   </xsl:apply-templates>
  </TRANSACTIONS>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<DATA RESULT="TRUE">
    <ACCOUNT NUMBER="MyNumber" CURRENCY="MyCurrency" TYPE="MyType" />
    <TRANSACTIONS>
        <TRANSACTION DESCRIPTION="DESC 1"
        CURRENCY="USD"
        POSTED-DATE="2010/01/31" AMOUNT="240.88" />
        <TRANSACTION DESCRIPTION="DESC 3"
        CURRENCY="USD"
        POSTED-DATE="2010/05/20" AMOUNT="240.88" />
        <TRANSACTION DESCRIPTION="DESC 8"
        CURRENCY="USD"
        POSTED-DATE="2008/12/31" AMOUNT="240.88" />
        <TRANSACTION DESCRIPTION="DESC 3"
        CURRENCY="USD"
        POSTED-DATE="2003/12/31" AMOUNT="240.88" />
        <TRANSACTION DESCRIPTION="DESC 99"
        CURRENCY="USD"
        POSTED-DATE="2011/01/01" AMOUNT="240.88" />
        <TRANSACTION DESCRIPTION="DESC 999"
        CURRENCY="USD"
        POSTED-DATE="2011/01/06" AMOUNT="240.88" />
    </TRANSACTIONS>
</DATA>

produces the wanted, correct result:

<DATA RESULT="TRUE">
   <ACCOUNT NUMBER="MyNumber" CURRENCY="MyCurrency" TYPE="MyType"/>
   <TRANSACTIONS>
      <TRANSACTION DESCRIPTION="DESC 3"
      CURRENCY="USD"
      POSTED-DATE="2003/12/31" AMOUNT="240.88"/>
      <TRANSACTION DESCRIPTION="DESC 8"
      CURRENCY="USD"
      POSTED-DATE="2008/12/31" AMOUNT="240.88"/>
      <TRANSACTION DESCRIPTION="DESC 1"
      CURRENCY="USD"
      POSTED-DATE="2010/01/31" AMOUNT="240.88"/>
      <TRANSACTION DESCRIPTION="DESC 3"
      CURRENCY="USD"
      POSTED-DATE="2010/05/20" AMOUNT="240.88"/>
      <TRANSACTION DESCRIPTION="DESC 99"
      CURRENCY="USD"
      POSTED-DATE="2011/01/01" AMOUNT="240.88"/>
      <TRANSACTION DESCRIPTION="DESC 999"
      CURRENCY="USD"
      POSTED-DATE="2011/01/06" AMOUNT="240.88"/>
   </TRANSACTIONS>
</DATA>
0

精彩评论

暂无评论...
验证码 换一张
取 消