2

我正在尝试根据帐号和与金额关联的货币添加交易。

这是原始 xml:我预计最多有 1000 个事务。在示例中,我有来自 2 个澳元和港币账户的 5 笔交易。我希望将每种货币、每笔交易的所有金额添加为 1 行。

<transactionlist>
    <transaction>
        <accountnumber>1</accountnumber>
        <amount>100</amount>
        <currency>AUD</currency>
    </transaction>
    <transaction>
        <accountnumber>1</accountnumber>
        <amount>50</amount>
        <currency>AUD</currency>
    </transaction>
    <transaction>
        <accountnumber>1</accountnumber>
        <amount>100</amount>
        <currency>HKD</currency>
    </transaction>
    <transaction>
        <accountnumber>1</accountnumber>
        <amount>500</amount>
        <currency>HKD</currency>
    </transaction>
    <transaction>
        <accountnumber>2</accountnumber>
        <amount>200</amount>
        <currency>AUD</currency>
    </transaction>
</transactionlist>

这是预期的输出(计数是添加了多少事务来创建新行):

<transactionlist>
    <row>
        <accountnumber>1</accountnumber>
        <totalamount>150</totalamount>
        <currency>AUD</currency>
        <count>2</count>
    </row>
    <row>
        <accountnumber>1</accountnumber>
        <totalamount>600</totalamount>
        <currency>HKD</currency>
        <count>2</count>
    </row>
    <row>
        <accountnumber>2</accountnumber>
        <totalamount>200</totalamount>
        <currency>AUD</currency>
        <count>1</count>
    </row>
</transactionlist>

这是我走了多远。只是不确定如何只为按帐户分组的每笔交易添加货币

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <transactionlist>
      <xsl:apply-templates select="transactionlist/transaction" />
    </transactionlist>
  </xsl:template>

  <xsl:template match="transaction">
    <!-- only do work for the *first* transaction with any particular ID -->
    <xsl:if test="not(preceding-sibling::transaction/accountnumber = current()/accountnumber)">
      <row>
        <xsl:copy-of select="accountnumber" />
        <totalamount>
          <xsl:call-template name="running-total-byaccount">
            <xsl:with-param name="values" select="/transactionlist/transaction[accountnumber = current()/accountnumber]" />
          </xsl:call-template>
        </totalamount>
      </row>
    </xsl:if>
  </xsl:template>

  <xsl:template name="running-total-byaccount">
    <xsl:param name="values" />
    <xsl:choose>
      <xsl:when test="count($values)">
        <xsl:variable name="curr" select="$values[1]" />
        <xsl:variable name="rest" select="$values[position() &gt; 1]" />
        <!-- recursive step: calculate the total of all remaining values -->
        <xsl:variable name="subtotal">
          <xsl:call-template name="running-total-byaccount">
            <xsl:with-param name="values" select="$rest" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="$subtotal + $curr/amount" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="0" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


</xsl:stylesheet>
4

1 回答 1

6

对于使用 XSLT 的命令式“类似编程”的方法,您已经走得太深了。我向我的学生建议,使用分组方法的声明式、自上而下的方法是解决诸如您的需求的最佳方法。

在 XSLT 1.0 的分组方法中,由于组的嵌套性质,您的问题向我建议了基于变量的分组方法。XSLT 2.0 使这更容易,但是您已经在样式表中声明了 XSLT 1.0,所以我认为这对您来说是一个限制。

我整理的一个快速解决方案在下面的成绩单中。我希望这有帮助。您可以看到它避免了所有递归问题并将需求分解为分组问题。

数据:

T:\ftemp>type currency.xml 
<transactionlist>
    <transaction>
        <accountnumber>1</accountnumber>
        <amount>100</amount>
        <currency>AUD</currency>
    </transaction>
    <transaction>
        <accountnumber>1</accountnumber>
        <amount>50</amount>
        <currency>AUD</currency>
    </transaction>
    <transaction>
        <accountnumber>1</accountnumber>
        <amount>100</amount>
        <currency>HKD</currency>
    </transaction>
    <transaction>
        <accountnumber>1</accountnumber>
        <amount>500</amount>
        <currency>HKD</currency>
    </transaction>
    <transaction>
        <accountnumber>2</accountnumber>
        <amount>200</amount>
        <currency>AUD</currency>
    </transaction>
</transactionlist>

执行和结果:

T:\ftemp>call xslt currency.xml currency.xsl 
<?xml version="1.0" encoding="utf-8"?>
<transactionlist>
   <row>
      <accountnumber>1</accountnumber>
      <totalamount>150</totalamount>
      <currency>AUD</currency>
      <count>2</count>
   </row>
   <row>
      <accountnumber>1</accountnumber>
      <totalamount>600</totalamount>
      <currency>HKD</currency>
      <count>2</count>
   </row>
   <row>
      <accountnumber>2</accountnumber>
      <totalamount>200</totalamount>
      <currency>AUD</currency>
      <count>1</count>
   </row>
</transactionlist>

样式表:

T:\ftemp>type currency.xsl 
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="transactionlist">
  <transactionlist>
    <xsl:variable name="trans" select="transaction"/>
    <xsl:for-each select="$trans">
      <!--find all unique accounts-->
      <xsl:if test="generate-id(.)=
             generate-id($trans[accountnumber=current()/accountnumber][1])">
        <xsl:variable name="acc" 
                    select="$trans[accountnumber=current()/accountnumber]"/>
        <xsl:for-each select="$acc">
          <!--find all unique currencies in the accounts-->
          <xsl:if test="generate-id(.)=
                        generate-id($acc[currency=current()/currency][1])">
            <!--note all for the given currency-->
            <xsl:variable name="curr"
                          select="$acc[currency=current()/currency]"/>
            <row>
              <xsl:copy-of select="accountnumber"/>
              <totalamount>
                <xsl:value-of select="sum($curr/amount)"/>
              </totalamount>
              <xsl:copy-of select="currency"/>
              <count>
                <xsl:value-of select="count($curr)"/>
              </count>
            </row>            
          </xsl:if>
        </xsl:for-each>
      </xsl:if>
    </xsl:for-each>
  </transactionlist>
</xsl:template>

</xsl:stylesheet>

已编辑:修复错别字并收集货币值以提高可读性。

于 2013-09-24T01:38:27.090 回答