[xsl] XSLT variables and for-each loop

Subject: [xsl] XSLT variables and for-each loop
From: Oleg Konovalov <olegkon@xxxxxxxxx>
Date: Fri, 13 May 2005 10:42:11 -0400
 Hi,

I am new to XSLT, trying to significantly modify somebody
else's XSL. That is not 2.0.

I need to create min & max variable(s)
to be used in many templates and sub-templates
based on certain conditions (not always).

It can't be calculated at the beginning of the file
(depends on other variables),
so seems like it has to be made local variable(s).

Since it's not easy to calculate (20-50 lines of code each)
probably makes sense to make a template out of it.

What I don't know is:
1) how to make template to return a value, something like
[that's not XSLT
2.0]:
 <xsl:variable min><call-template
name="calculate-min"/></xsl:variable>

2) how to declare a global variable and then assign it much
later in the
code;

3) Also, it there a way to compare a node value with a previous
value within a for-each loop like (to find a max value of the
nodes):
<xsl:for-each mystruct/mystuct1>
 <xsl:if test="position() &gt; 1  and
   var2 &gt; var2(position()-1)">...do something...
 </xsl:if>
</xsl:for-each>
Would that work ?

4) Can I get access to a value from another branch
from within for-each (where var2 and var3 are "brothers")?

Imagine you have an XML tree which has the structure like:
mystruct/myvar1[i]/myvar/var2 and
mystruct/myvar2[i]/myvar/var3

I need to compare the values of them for the same index "i".
Can I do it in for-each loop like that:
<xsl:for-each mystruct/myvar1[i]/myvar>
  <xsl:if test="number(var2) &gt; number(../../myvar2[i]/myvar/var3)">
   ...do something...
  </xsl:if>
</xsl:for-each>)

Any help is very appreciated.

Thank you in advance,
Oleg

Current Thread