Re: [xsl] For-each loop or recursion

Subject: Re: [xsl] For-each loop or recursion
From: David Carlisle <davidc@xxxxxxxxx>
Date: Sat, 14 May 2005 11:02:18 +0100
> I am new to XSLT, modifying somebody else's code:
You probably don't really need to cross post to 5 newsgroups as well as
this list though.


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


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

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

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

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread