累計算出テンプレート

テーブル表示するときに累計も算出して欲しいなーというときに利用するといいかな…と。
あんまりお利口さんではないXSLテンプレートの作りかたしていますので、もうちょっとスマートな方法があればうれしいなぁ。

XSL

<xsl:template name="accumulating_total">
  <xsl:param name="nodes"></xsl:param>
  <xsl:param name="pos"></xsl:param>
  <xsl:choose>
   <xsl:when test="$pos=1"><xsl:value-of select="$nodes[1]"></xsl:value-of></xsl:when>
   <xsl:otherwise>
    <xsl:variable name="recursive_result">
    <xsl:call-template name="accumulating_total">
     <xsl:with-param name="nodes" select="$nodes"></xsl:with-param>
     <xsl:with-param name="pos" select="number($pos)-1"></xsl:with-param>
    </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$nodes[$pos]+$recursive_result"></xsl:value-of>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

サンプル
XML

<application_list>
 <record>
  <accounting_byperiod>1</accounting_byperiod>
  <counts>0</counts>
  <amounts>0</amounts>
 </record>
 <record>
  <accounting_byperiod>2</accounting_byperiod>
  <counts>0</counts>
  <amounts>0</amounts>
 </record>
 <record>
  <accounting_byperiod>3</accounting_byperiod>
  <counts>4</counts>
  <amounts>35800</amounts>
 </record>
 <record>
  <accounting_byperiod>4</accounting_byperiod>
  <counts>1</counts>
  <amounts>2000</amounts>
 </record>
 <record>
  <accounting_byperiod>5</accounting_byperiod>
  <counts>0</counts>
  <amounts>0</amounts>
 </record>
</application_list>

利用方法サンプルXSL

<table border="1" cellspacing="0" cellpadding="0" width="95%">
 <tr bgcolor="#FFFF99">
  <th width="20%">年月</th>
  <th width="15%">件数</th>
  <th width="15%">件数累計</th>
  <th width="25%">金額</th>
  <th width="25%">金額累計</th>
 </tr>
 <xsl:for-each select="/application_list/record">
  <tr><td nowrap="nowrap" align="center"><xsl:value-of select="accounting_byperiod"></xsl:value-of></td>
   <td nowrap="nowrap" align="center"><xsl:value-of nowrap" align="center" select="counts></xsl:value-of></TD>
   <TD nowrap=">
   <xsl:variable name="accumulating_total_counts">
    <xsl:call-template name="accumulating_total">
     <xsl:with-param name="nodes" select="//counts"></xsl:with-param>
     <xsl:with-param name="pos" select="position()"></xsl:with-param>
    </xsl:call-template></xsl:variable>
   <xsl:value-of select="$accumulating_total_counts"></xsl:value-of></td>
   <td nowrap="nowrap" align="right"><xsl:value-of select="format-number(amounts, '#,##0')"></xsl:value-of></td>
   <td nowrap="nowrap" align="right">
   <xsl:variable name="accumulating_total_amount">
    <xsl:call-template name="accumulating_total">
     <xsl:with-param name="nodes" select="//amounts"></xsl:with-param>
     <xsl:with-param name="pos" select="position()"></xsl:with-param>
    </xsl:call-template></xsl:variable>
   <xsl:value-of select="format-number($accumulating_total_amount, '#,##0')"></xsl:value-of></td>
  </tr>
 </xsl:for-each>
</table>