Re: [xsl] XSLT variables and for-each loop

Subject: Re: [xsl] XSLT variables and for-each loop
From: omprakash.v@xxxxxxxxxxxxx
Date: Fri, 13 May 2005 20:36:46 +0530
Hi,
    See below. Things like returing values are done in procedural languages
and XSLT is a lot of things but being procedural is not one of those.
Therefore, the concept of returing a scalar like as when you call a
function is not applicable in XSLT.

how to make template to return a value, something like

1. <xsl:variable name="min">
<<xsl:call-template name="calc-min"/>
</xsl:variable>

That template is valid in XSLT though what it does is binds a fragment of a
tree created inside the template named calc-min to the variable named min.


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

Cannot be done. Variables contrary to their name are constants and once
assigned their values cannot be modified.

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 ?

Sure. The above would work provided you follow the syntax for referring to
preceding nodes. Check out on axis like preceding, following etc in a good
XSLT book or on the net. And trying to implement a bubble sort in xslt like
you do could get out of hand quickly. Rely on the xsl:sort instruction
instead.

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>)

Sure. That is what XSLT is meant for. What you say at the same position is
referred to in XPATH as the index.
Again you need to use the axis to refer to the node you want.

Hope this helps.

Cheers,
Omprakash.V









                                                                                                                   
                    Oleg                                                                                           
                    Konovalov            To:     xsl-list@xxxxxxxxxxxxxxxxxxxxxx                                   
                    <olegkon@gmai        cc:     (bcc: omprakash.v/Polaris)                                        
                    l.com>               Subject:     [xsl] XSLT variables and for-each loop                       
                                                                                                                   
                    05/13/2005                                                                                     
                    08:12 PM                                                                                       
                    Please                                                                                         
                    respond to                                                                                     
                    xsl-list                                                                                       
                                                                                                                   
                                                                                                                   




 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






This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. 
If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately.
You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification,
distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit Us at http://www.polaris.co.in

Current Thread