This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Adding numbers in xsl loop


Hello Ard,

> Apparently, this does not work in xsl. I can't figure out, how to
> use a basic programming issue like i = i + 1 in a loop???????????

In general, because XSLT is a declarative programming language rather
than a procedural one, you have to use recursion to handle problems
like this.

In your case, you want to iterate over a set of projects and add the
associated durations to an ongoing total. You can do this with a
recursive template such as the following:

<xsl:template name="addDurations">
  <xsl:param name="projects" />
  <xsl:param name="entries" />
  <xsl:param name="absTotal" select="0" />
  <xsl:choose>
    <xsl:when test="$projects">
      <xsl:variable name="totaalX"
        select="sum($entries[projectname =
                             $projects[1]/name]/duration)" />
      <xsl:call-template name="addDurations">
        <xsl:with-param name="projects"
                        select="$projects[position() > 1]" />
        <xsl:with-param name="entries" select="$entries" />
        <xsl:with-param name="absTotal"
                        select="$absTotal + $totaalX" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$absTotal" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

This template works through the $projects one by one, using the first
project in the list to identify the relevant entries and passing the
rest through to the next recursion of the template. Each time, it adds
the durations to the $absTotal; when all the projects have been looked
at and the list is empty, it gives this total.

You can call this template with the following:

  <xsl:call-template name="addDurations">
    <xsl:with-param name="projects"
      select="projects/project[medewerker[login=$loginMatch]]" />
    <xsl:with-param name="entries"
      select="hours/entry[login=$loginMatch]
                         [date/year=$jaarX][date/month=$maandX]" />
  </xsl:call-template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]