Ms.D
Ms.D

Reputation: 717

XSLT: Mathematical addition of elements based on 2 child elements

I am trying to add transactions based on the account number and the currency associated with the amount.

Here is the original xml: I expect there to be at most 1000 transactions. In the example I have 5 transactions from 2 accounts in AUD and HKD. I would like all the amounts, per currency, per transaction to be added as 1 line.

<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>

This is the expected output (count being how many transactions were added to create that new row):

<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>

Here is how far I got. Just not sure how to go about only adding by the currency for each transaction grouped by account

<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>

Upvotes: 2

Views: 10576

Answers (1)

G. Ken Holman
G. Ken Holman

Reputation: 4403

You've gone far too deep with an imperative "programming-like" approach to using XSLT. I recommend to my students that a declarative, top-down approach using a grouping methodology is the best way to approach a requirement such as yours.

Of the grouping methodologies for XSLT 1.0, your problem suggests to me the variable-based grouping methodology because of the nested nature of the groups. XSLT 2.0 makes this easier, but you've declared XSLT 1.0 in your stylesheet so I've assumed that is a limitation for you.

A quick solution I threw together is in the transcript below. I hope this helps. You can see it avoids all the issues of recursion and breaks down the requirement into a grouping problem.

Data:

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>

Execution and results:

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>

Stylesheet:

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>

Edited: repaired typos and collected currency values for readability.

Upvotes: 6

Related Questions