[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] For-each loop or recursion


Subject: Re: [xsl] For-each loop or recursion
From: omprakash.v@xxxxxxxxxxxxx
Date: Mon, 16 May 2005 09:35:08 +0530

Hi Oleg,

 "legal XPath expression that selects all elements called myarray that
 have a child element called i."

Iam sure Dave had his reasons for his above statement. Below is my
understanding of the problem.

myarray[i] selects the i'th myarray child of the context node. So if your
xml looks like this:


<mystruct>
<myarray1>
<myvar>
<var2>
5
</var2>
</myvar>
</myarray1>
<myarray2>
<myvar>
<var3>
6
</var3>
</myvar>
</myyarray2>
</mystruct>

The xpath mystruct/myarray1[i]/myvar/var2 selects 5 where i=1
and
mystruct/myarray2[i]/myvar/var3 selects 6 where i = 1

Is this what you meant by "parallel branches". In XSLT, they are called
siblings. Thus myarray1 and myarray2 are sibings and are children of
mystruct and
myarray2 is the first in the following-sibling axis of myarray1 and so on.

Hope this helps clarify things a bit.

Cheers,
Omprakash.V







                                                                                                                   
                    Oleg                                                                                           
                    Konovalov            To:     xsl-list@xxxxxxxxxxxxxxxxxxxxxx                                   
                    <olegkon@gmai        cc:     (bcc: omprakash.v/Polaris)                                        
                    l.com>               Subject:     Re: [xsl] For-each loop or recursion                         
                                                                                                                   
                    05/16/2005                                                                                     
                    09:18 AM                                                                                       
                    Please                                                                                         
                    respond to                                                                                     
                    xsl-list                                                                                       
                                                                                                                   
                                                                                                                   




David,

Answering your questions:

> > I have the following data (leaves in parallel branches):
> > mystruct/myarray1[i]/myvar/var2 and
> > mystruct/myarray2[i]/myvar/var3
>
> It isn't clear what you mean by the myarray[i] construct, that is a
> legal XPath expression that selects all elements called myarray that
> have a child element called i. I suspect that isn't what you want,
> I could make some guesses as to what you do mean but in any case that
> presumably is not your data, but rather a sketch of part of your
> stylesheet accessing the data. You need to say what your input looks
like.

To be very honest, I am not totally sure myself about myarray1[i].
As I said, that is not my code (author is long gone), and I am new to XSLT.

I am just trying to make enhancements. That is how that node is
accessed everywhere.
In fact, it is mostly myarray1[1] and myarray2[1],
what you can call "parallel branches".

>
> > I need to implement the find the first occurence of :
> > <xsl:if test="position() != last() and
> >   number(var2) = number(var2[position()+1]) and
> >   number(var3) = number(var3[position()+1])">
> >       <value-of select="position()">
> > </xsl:if>
>
> Again I'm struggling to guess what you mean here. You need to describe
> the transformation that you want to do, either in english or by posting a
> small (six line) example input and stating what result you want to get
> from that input.
>
> the above is syntactically legal Xpath but
> var2[position()+1] is short for var2[position()=position()+1]
> so selects the set of var2 elements for which the position is equal to
> one more than the position so this is the empty set.
> applying number() to that always produced NotaNumber. NaN is never = to
> any other value (including itself) so the two = tests in the expression
> will never be true so the xsl:if will never return anything.

I need to compare current and next values of the node var2 [and var3]
and find the first occurrence when var2[position()] = var2[next, i.e.
position()+1]
Are you saying that is impossible to do in XSLT ?
If not, how should I do it ?


> > 2) Will I be able to get a node from the parallel branch in for-each
loop ?
> > Something like:
> > <xsl:for-each mystruct/myarray1[i]/myvar>
> >   <xsl:if test=" ...and number(../../myarray2[i]/myvar/var3) =
> > number((../../myarray2[i]/myvar/var3)[position()+1]) and...">
> >     <value-of select="position()">
> >   </xsl:if>
> > </xsl:for-each>)
> > I know it looks awful  :-(
> As in the other cases this is syntactically legal but probably not what
> you want to do, but it doesn't give us any indication of what you _do_
> want to do.

I want from inside for-each loop mystruct/myarray1[i]/myvar
[or does it require recursion?]
compare the values is the parallel branch,
of parallel "current" node ../../myarray2[i]/myvar/var3 and its "next
node".
Again, where:
mystruct/myarray1[i]/myvar/var2 and
mystruct/myarray2[i]/myvar/var3


> > 3) Is there a way to somehow start the for-each loop
> > from position other than 1 ?
>
> for-each isn't looping over some counter like a Fortran DO loop, it is
> iterating over a set of nodes, position() reports the order in which the
> nodes are being processed so obviously it alwyas starts with
> position()=1 This position() value has no relationship with any position
> of the node in the document tree: you can select any nodes (and using
> xsl:sort) process them in any order.
> for example if your current node has 10 child elements called x and you
> go
> <xsl:for-each select="x[position()&gt;3]">
> then the first node processed will be the 4th x child, but position()
> will be equal to 1 in that first iteration.

Not sure about Fortran, I am from Java world.

Is there an easy way to find out the max value of the node (child elements)
without sorting it ?
The sequence of nodes I am dealing with is supposed to be in increasing
order,
but might repeat the particular node. So I am in fact looking for the
1st occurrence
of max  value in 2 branches to say "6 (max) or more" and not to
display the rest.
FYI: displaying the table with value rebates for particular product,
so now it looks like:

                    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

Since rebate1 and rebate2 are coming from totally different
database tables [and calculated based on totally different criteria],
they are put in different XML structures, but happen to be on the
same nesting level.

Do you understand what I am doing now ?

What is the easiest way to achieve it, via for-loop or recursion ?


Thank you.
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
Keywords