在 XSLT 中排序不起作用

Sorting in XSLT not working

我想根据作为帐户元素的余额对原始 XML 中的帐户进行排序。我写的代码不起作用,不知道为什么。我认为是正确的。你能帮我解决我可能错的地方吗?

这是我的原始 XML 文件的一部分。

<account actype="Current" acid="ac_10100001234" branch-id="br_101" cid="c_100101">
    <open-date>25/5/2013</open-date>
    <close-date>31/03/2015</close-date>
    <balance>5000</balance>
</account>
<account actype="Saving" acid="ac_10100002123" branch-id="br_101" cid="c_100101">
    <open-date>12/5/2012</open-date>
    <close-date>12/11/2014</close-date>
    <balance>8000</balance>
</account>
<account actype="Recurring" acid="ac_12051001" branch-id="br_1205" cid="c_100102">
    <open-date>22/5/2014</open-date>
    <close-date>21/5/2019</close-date>
    <balance>4200</balance>
</account>
<account actype="Fixed" acid="ac_1012052113" branch-id="br_101" cid="c_100102">
    <open-date>20/10/2009</open-date>
    <close-date>19/10/2014</close-date>
    <balance>100000</balance>
</account>
<account actype="Current" acid="ac_100100001234" branch-id="br_1100" cid="c_1001100">
    <open-date>15/5/2010</open-date>
    <close-date>12/4/2014</close-date>
    <balance>300</balance>
</account>

这显示了我如何在帐户上调用我的模板。

<xsl:stylesheet 
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
>
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/Bank">
    <root>
      <name>
        <xsl:value-of select="name" />
      </name>
      <xsl:apply-templates select="branch" />
      <xsl:apply-templates select="employee" />
      <xsl:apply-templates select="customer" />
      <xsl:apply-templates select="account" />
      <xsl:apply-templates select="transaction" />
    </root>
  </xsl:template>
  <xsl:template match="account">
    <xsl:for-each select=".">
      <xsl:sort select="balance" />
      <Account>
        <AccountNumber>
          <xsl:value-of select="@acid" />
        </AccountNumber>
        <Customerid>
          <xsl:value-of select="@cid" />
        </Customerid>
        <balance>
          <xsl:value-of select="balance" />
        </balance>
      </Account>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

要实现排序输出,您需要按特定顺序应用模板。模板无法自行排序。

<xsl:stylesheet 
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
>
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/Bank">
    <root>
      <xsl:copy-of select="name" />
      <xsl:apply-templates select="branch" />
      <xsl:apply-templates select="employee" />
      <xsl:apply-templates select="customer" />
      <!-- here! -->
      <xsl:apply-templates select="account">
        <xsl:sort select="balance" data-type="number" />
      </xsl:apply-templates>
      <xsl:apply-templates select="transaction" />
    </root>
  </xsl:template>

  <xsl:template match="account">
    <Account>
      <AccountNumber>
        <xsl:value-of select="@acid" />
      </AccountNumber>
      <Customerid>
        <xsl:value-of select="@cid" />
      </Customerid>
      <xsl:copy-of select="balance" />
    </Account>
  </xsl:template>
</xsl:stylesheet>

备注

  • data-type。这默认为 "string",不会正确排序数字。
  • <xsl:copy-of>
  • 您似乎没有使用 xs 命名空间,您可以删除声明。