How to apply aggregation function sum() on attribute

I want to sum up all the attribute value. I have tried the following query but it doesn’t work.

for $i in input()/bank/client/account/depositor
return sum({data($i/@amount)})

the output is:
sum(100)
sum(500)
sum(240)

My expected output is 840

Can anyone help me?

The result you are getting is because you are summing each resulting document from the ‘for’ so the result is correct. To get a sum of all @amount attributes you need to reformulate the query thus:

let $values := (for $doc in input()/bank return xs:integer($doc/client/account/depositor/@amount))
return xs:integer(sum($values))

This way the sum function operates on a array of integer values and returns an integer result.

Hope this helps.