I can.
but before I do, let me give some remarks:
I do not know where you got this XQuery from. Either it is a puzzle , or something programmed by an XQuery novice. Judging from the variable names, what is intended to be achieved can be achieved in a much simpler way with XQuery. Besides, the query does not do what the variable names suggest. And it will create a type exception. And it does not conform to XQUery as of teh current spec and Tamino 4.4. implementation. Here we go:
declare function xx:count-nodes($sequence)
{
if ($sequence) then
( let $head := ( for $e1 in $sequence
let $other := ( for $e2 in $sequence
return ( if (not($e1 is $e2)) then $e2
else () ) )
return if ( for $e3 in $other
return if ($e3 << $e1) then 1
else () ) then $e1
else () )
return let $tail := ( for $e1 in $sequence
return if (not($e1 is $head)) then $e1
else () )
return (1 + xx:count-nodes($tail)) )
else 0}
from the name of the function, we can assume that it is intended to
count the nodes in a sequence. The fact that all entries of the sequence
are compared using the “is” operator also implies that the sequence consists of nodes only (otherwise you get a type exception, because the
“is” operation is defined on nodes only.
the outermost if says:
if the incoming sequence is empty, return 0
so for the empty sequence, teh number of nodes is correctly returned.
In case the sequence is not empty:
a variable $head is defined. However, in contrast to its name, it will contain a sequence of all nodes that are not identical to the first node of the sequence (in document order, i.e. not the first entry in the sequence):
This is the reason for this result:
for $e1 in $sequence
let $other := ( for $e2 in $sequence
return ( if (not($e1 is $e2)) then $e2
else () ) )
for each entry i$e1 n the sequence, the variable $other is computed.
it will contain a sequence of all nodes which are not identical to $e1
return if ( for $e3 in $other
return if ($e3 << $e1) then 1
else () )
then $e1
else () )
means that whenever there is a node is before $e1 in document order, the argument of teh if evaluates to true, and $e1 is returned, i.e. , all nodes are returned that have a predecessor in document order in the sequence (not really what is implied by the name $head)
return let $tail := ( for $e1 in $sequence
return if (not($e1 is $head)) then $e1
else () )
will fails because of a type error, whenever the input sequence has more than two different nodes as entries, because then $head is a non-atomic sequence which creates a type error if operator “is” is applied to it.
The intention seems to be: compute all node except the $head one.
As there is a built-in count function, I wonder what the intention of this question to the forum is
Regards
Harald