Subject: Re: Trying to undestand why template works
From: "Jon Gorman" <jonathan.gorman@xxxxxxxxx>
Date: Thu, 23 Feb 2006 12:31:43 -0600
|
On 2/23/06, yguaba@xxxxxxxxxxxx <yguaba@xxxxxxxxxxxx> wrote:
> Hello,
>
> After some digging and experimenting, I finally got a stylesheet to
> do what I wanted. Among other things, it uses the template below.
> Problem is, I don't really understand WHY this template works, and
> would very much like to.
Ooooohhhh boy. The random code messing to what might work.
> Doesn't "position() > 1" evaluate to either 0 or 1?
Errr, no. It would either be true or false. ie
select="foo[position() > 1]"
can be read as "get all the children element of the current node that
are named foo and also have a position greater than one".
Since you are adding them in a sequence, you are adding the one with
position 1 to the result of the recursive call, so you only want those
that are after position 1.
I'm not sure if you are confusing the [] with some sort of array call.
It's a conditional, so using foo[1] is really short for
foo[position() = 1], which could actually be expanded more as well.
The other thing that might be confusing you is how you're manipulating
the node-set.
Say you have something like
<foo>
<el>1</el>
<el>2</el>
<el>3</el>
</foo>
The first time you call this function, you're passing in the node-set
(called node, a bit confusing). It contains three nodes, all of them
el.
Then the recursive function finds the value of the first el +
sum-value(second el, third el)
this is because both the second el and third el have positions greater
than 2, so they are put into a new node-set and passed along with the
$node param.
second pass
value of second el + sum-value (third el)
In the new node set, only the third el has a position greater than one
third pass
value of third + sum-value ()
now the node-set only has the third el, which has a position of 1
fourth pass
base case, return 0
empty node-set, return 0
0 + 3
3 + 2
5 + 1
6
Does any of this help?
Jon Gorman
|