## RE: [xsl] Calculating min/max values for sum of different node va lues

 Subject: RE: [xsl] Calculating min/max values for sum of different node va lues From: bryan.s.schnabel@xxxxxxxxxxxxxx Date: Wed, 21 May 2003 16:42:52 -0700
```I got a bit lost when it came down to what you wanted in the end.  But you
could try a brute force approach.

<xsl:template match="graph">
<sets>
<a set="{\$startYear}">
<xsl:for-each select="year[\$startYear=@value]/return[(@month &gt;
(\$startMonth - 1))
and (@month &lt;
(\$endtMonth + 1))]">
<xsl:sort />
<xsl:if test="position()=1">
<min>
<xsl:value-of select="." />
</min>
</xsl:if>
<xsl:if test="position()=last()">
<max>
<xsl:value-of select="." />
</max>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="year[\$startYear=@value]">
<sum>
<xsl:value-of select="sum(return[(@month &gt; (\$startMonth - 1))
and (@month &lt; (\$endtMonth + 1))])" />
</sum>
</xsl:for-each>
</a>
<b set="{\$endYear}">
<xsl:for-each select="year[\$endYear=@value]/return[(@month &gt;
(\$startMonth - 1))
and (@month &lt;
(\$endtMonth + 1))]">
<xsl:sort />
<xsl:if test="position()=1">
<min>
<xsl:value-of select="." />
</min>
</xsl:if>
<xsl:if test="position()=last()">
<max>
<xsl:value-of select="." />
</max>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="year[\$endYear=@value]">
<sum>
<xsl:value-of select="sum(return[(@month &gt; (\$startMonth - 1))
and (@month &lt; (\$endtMonth + 1))])" />
</sum>
</xsl:for-each>
</b>
</sets>
</xsl:template>

Which results in:

<sets>
<a set="2001">
<min>1</min>
<max>2.5</max>
<sum>6.7</sum>
</a>
<b set="2002">
<min>1</min>
<max>2.5</max>
<sum>6.5</sum>
</b>
</sets>

-----Original Message-----
From: Bogdan Constantinescu [mailto:bconstantinescu@xxxxxxxxxxxxxx]
Sent: Wednesday, May 21, 2003 9:35 AM
To: XSL-List (E-mail)
Subject: [xsl] Calculating min/max values for sum of different node
values

Hi,

Here is a sample of the xml file that I am using:

<graph>
<year value="2000">
<return month="1">1</return>
<return month="2">-2.5</return>
<return month="3">2</return>
<return month="4">3</return>
<return month="5">4</return>
<return month="6">2</return>
<return month="7">-1</return>
<return month="8">0</return>
<return month="9">1</return>
<return month="10">9</return>
<return month="11">5</return>
<return month="12">0</return>
</year>
<year value="2001">
<return month="1">0</return>
<return month="2">1</return>
<return month="3">2.2</return>
<return month="4">1</return>
<return month="5">2.5</return>
<return month="6">1</return>
<return month="7">3</return>
<return month="8">2</return>
<return month="9">1</return>
<return month="10">6</return>
<return month="11">2.1</return>
<return month="12">1</return>
</year>
<year value="2002">
<return month="1">0</return>
<return month="2">1</return>
<return month="3">2</return>
<return month="4">1</return>
<return month="5">2.5</return>
<return month="6">1.5</return>
<return month="7">-3</return>
<return month="8">2</return>
<return month="9">1</return>
<return month="10">6</return>
<return month="11">2.1</return>
<return month="12">1</return>
</year>
</graph>

What I want to do is the following:

I will have 4 variables defined in the XSL:

<xsl:variable name="startYear">2001</xsl:variable>
<xsl:variable name="endYear">2002</xsl:variable>
<xsl:variable name="startMonth">2</xsl:variable>
<xsl:variable name="endtMonth">5</xsl:variable>

which will get each time different values from a configuration file. Let's
use these hardcoded values for now.

I want to calculate the min/max (and store the results in some variables for
later use) for the sums of all the return values that are in the specified
range. For example, in this case I want to sum all the return values from
year 2001 that have the month attribute between 2 and 5, and then calculate
the sum for all the return values from year 2002 that have the month
attribute between 2 and 5 and in the end calculate the max/min of these 2
sums.

Does anybody have any idea if I can do this in XSL ?

Thanks a lot for your help !

Bogdan Constantinescu

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

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

```