I have the following XML:

<?xml version = "1.0"?>
<transactions>
<transaction date = "05/22/2000" id = "0122">
<from account = "100392"/>
<to account = "203921"/>
<amount currency = "USD">15</amount>
</transaction>
<transaction date = "06/01/2000" id = "0129">
<from account = "203921"/>
<to account = "877521"/>
<amount currency = "USD">4800</amount>
</transaction>
<transaction date = "06/01/2000" id = "0130">
<from account = "100392"/>
<to account = "992031"/>
<amount currency = "YEN">7000</amount>
</transaction>
<transaction date = "06/10/2000" id = "0152">
<from account = "992031"/>
<to account = "100392"/>
<amount currency = "USD">402.53</amount>
</transaction>
<transaction date = "06/22/2000" id = "0188">
<from account = "100392"/>
<to account = "203921"/>
<amount currency = "USD">10000</amount>
</transaction>
<transaction date = "07/12/2000" id = "0200">
<from account = "100392"/>
<to account = "039211"/>
<amount currency = "NTD">3000</amount>
</transaction>
<transaction date = "07/26/2000" id = "0211">
<from account = "203921"/>
<to account = "100392"/>
<amount currency = "USD">400</amount>
</transaction>
<transaction date = "08/05/2000" id = "0225">
<from account = "039211"/>
<to account = "203921"/>
<amount currency = "USD">150</amount>
</transaction>
<transaction date = "09/03/2000" id = "0293">
<from account = "100392"/>
<to account = "039211"/>
<amount currency = "NTD">200000</amount>
</transaction></transactions>


I have found all the transactions 'to' account 203921 and returned them all as one value:

sum(/transactions/transaction[to/@account="203921"]/self::*/number(amount))

I want to be able to divide this result by the number of nodes found (I.E 3 in this case) (Found 3 account ="203921") How can I do this? I've tried numerous ways, but to no success.

Thanks.

There are many ways to solve this problem. It can be solved using a for-each loop but the preferred XSLT way is to use recursion. The following solution uses Tail Recursion which is an optimized version of recursion.

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


  <xsl:output method="text"/>

  <xsl:template match="/" >
     <!-- use tail recursion to optimize -->
     <xsl:call-template name="countSum" >
        <xsl:with-param name="list" select="//transaction[to/@account='203921']" />
     </xsl:call-template>
  </xsl:template>

  <xsl:template name="countSum">
     <xsl:param name="list" />
     <xsl:param name="sum" select = "0" />
     <xsl:param name="count" select= "0" />

     <xsl:choose>
        <xsl:when test="$list">
            <xsl:call-template name="countSum">
               <xsl:with-param name="list" select="$list[position() > 1]" />
               <xsl:with-param name="sum" select="$sum + $list[1]/amount" />
               <xsl:with-param name="count" select="$count + 1" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
             SUM: <xsl:value-of select="$sum" />
             COUNT: <xsl:value-of select="$count" />
             <xsl:text>
</xsl:text>
        </xsl:otherwise>
     </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

Other optimizing recursion algorithms include: (1) Divide and Conquer, (2) Segmentation, and (3) Divide and Conquer with Segmentation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.