A: 

I'm a relative newcomer to XQuery myself, but one of the things I was surprised about was the lack of a map() function, as in LISP, to take a sequence of values and combine them into one, passing in the combining function. In LISP, I think you'd want something like "(map '+ (0 0 0 1 1 2 2 3 1 2))". You can do something similar in XQuery, but you have to write it yourself:

declare function local:sum-times($seq)
{
  if (count($seq) < 2)
  then
    subsequence($seq, 1, 1)
  else
    subsequence($seq, 1, 1) + local:sum-times(subsequence($seq, 2))
};

So then you can write "local:sum-times(for $i in fn:doc()...)". Basically, this recreates the sum() function you mentioned, but now you can make the changes you need.

Dave Cassel
A: 

its looks like you want to use fn:sum

like this:

fn:sum(
    (
        1, 1, 1, 1, 0, 0, 1, 0
    ), 
    0
)

More information here: http://www.xqueryfunctions.com/xq/fn_sum.html

Bill