Re: [xsl] Problem accumulating values.

Subject: Re: [xsl] Problem accumulating values.
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Thu, 3 Mar 2011 11:43:58 +0000
> The _problem_ is, that I need to calculate at the end of each week, the
> total duration time of all the tasks for that week (week = duration time
> of task 1 + duration time of task 2 + duration time of task 3...). I
> have tried using the sum() function in different ways but did not
> worked. I also tried creating a new tag inside my 'task' node, but it
> just does not work, sum() keeps on appending the values instead of
> summing them.

Sum is correct, for example:

sum(for $x in //task return $x/xs:dateTime(@end) - $x/xs:dateTime(@start))

returns

PT1M28S

> This is how my .XSLT file looks like: http://pastebin.com/jLV2EHFa
>
> and this is the result generated file: http://pastebin.com/dDpJVqLL
>
> As you see, the results are appended and not summed, which is what I
> need.

You are working at the <task> level, so you are outputting the sum of
just that 1 task.  You need to operate at the week level, eg change
the for-each in:

<xsl:template match="year/week">
  ....
  <xsl:for-each select="day/task">
    <xsl:call-template name="total_duration"/>
  </xsl:for-each>

to just:

<xsl:value-of select="sum(for $x in task return $x/xs:dateTime(@end) -
$x/xs:dateTime(@start))/>

(well, make it a variable and then do your formatting as you have done already)


-- 
Andrew Welch
http://andrewjwelch.com

Current Thread