我正在尝试根据帐号和与金额相关联的货币添加交易。
这是原始的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>
答案 0 :(得分:4)
对于使用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>
编辑:修复拼写错误并收集货币值以便于阅读。