我正在尝试根据帐号和与金额关联的货币添加交易。
这是原始 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() > 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>