# Re: For-each loop or recursion

 Subject: Re: For-each loop or recursion From: Oleg Konovalov Date: Mon, 16 May 2005 11:27:47 -0400
```Aron,

Existing code is very complex - big Cocoon project
(and I started using XSLT 2 weeks ago, but have lots of experience in
Java/C/C++),
so I tried to keep concentrated on what I am doing:
creating 2 templates to calculate the number of the first
column to be displayed [first non-zero on either rebate1 and rebate2]
and the last one [first occurrence of max number in either rebate1 or
rebate2]

1    2   3   4  5   6    7   8   9  10  11  12
13 (sales volume)
rebate1(%)     0   0   1   2   3   4    5   6   7   8    8    8    8
rebate2(%)     0   1   2   3   4   5    6   7   9   10  11  11  11

Need it be dynamically trimmed to display:

2   3   4  5   6    7   8   9  10  "11 or more"
rebate1(%)     0   1   2   3   4    5   6   7   8    8
rebate2(%)     1   2   3   4   5    6   7   9   10  11

And var2 is rebate1, var3 is rebate2

So if it makes it any easier, we can call it
mystruct/myarray1[1]/myvar/rebate1 and
mystruct/myarray2[1]/myvar/rebate2

At this point I am interested in how to implement algorithms in XSLT:
a) find out first non-zero on either rebate1 and rebate2
b) find out first occurrence of max number in either rebate1 or rebate2

So back to my questions:
1) How do I get a position of last occurrence of min ?

1a) Can I scan using for-each loop from the end of the sequence
to the beginning ?

2) OK, lets try to get a position of the first occurrence when var2 = max
node.
What would be a correct ?

<xsl:for-each select=mystruct/myarray1[1]/myvar>
<xsl:if select="rebate1=*[last()]">
<xsl:value-of select="position()">
</xsl:if>
</xsl:for-each>

3) Expanding 2 for the rebate2 (var3) for the values from another
(parallel) branch,
would that work ?

<xsl:for-each select=mystruct/myarray1[1]/myvar>
<xsl:if select="rebate1=*[last()] and
../../../myarray2[1]/myvar/rebate2=*[last()]">
<xsl:value-of select="position()">
</xsl:if>
</xsl:for-each>

I think, that second *[last()] is wrong,
I have to get a max of the rebate2 (second sequence)
before that for-each loop.

3a) Can I calculate max of rebate2 like:
<xsl:variable name="max2"
select="mystruct/myarray2[1]/myvar/rebate2[position()=last()]">
Is that the right way of assigning *[last()] to a variable ?

All that is based on the assumption that rebate1 and rebate2 sequences are

Thank you,
Oleg.

On 5/16/05, Aron Bock <aronbock@xxxxxxxxxxx> wrote:
> Oleg, please do really consider posting a sample of your XML; if your
actual
> XML is too verbose, make up a 5-liner that conceptually represents what you
> want to do.  And, no, you likely don't need loops or recursion.
>
> I wasn't going to post after *my* response with made-up data describing
> accessing sibling values, but figured I would now even just to point out
> that else, as you've seen, this can be slow going.
>
> Regards,
>
> --A
>
> >From: Oleg Konovalov <olegkon@xxxxxxxxx>
> >To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> >Subject: Re:  For-each loop or recursion
> >Date: Mon, 16 May 2005 09:41:37 -0400
> >
> >How do I get a position of last occurrence of min ?
> >
> >Do I have to use recursion?
> >Can I scan using for-each from the end of the sequence
> >to the beginning ?
>
> _________________________________________________________________
> Is your PC infected? Get a FREE online computer virus scan from McAfee(r)

```

### PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!