About sum function in xquery

Hi!

I’m having some problems while trying to make a ‘sum’. Let’s take a look to the query:

------------------------------------------------------------------------------
declare namespace xs=“XML Schema
let $nodo :=
for $cial in (‘7495’,‘10485’)
let $nodo_dia := input()/ageing[@id=‘2004-08-10’]
let $t1 := $nodo_dia/drv/comercial[@cod=$cial]/@tramo_1
return {string($t1)}
return

{$nodo/t1}

------------------------------------------------------------------------------

I get the next result:

------------------------------------------------------------------------------
<xq:result xmlns:xq=“http://namespaces.softwareag.com/tamino/XQuery/result”>

34673.95
22261.36

</xq:result>
------------------------------------------------------------------------------

But what I really want is make a sum of ‘t1’ with {sum($nodo/t1} and I get a Run Type Error.

Can anybody help me with this?

Thank you
Xavier Casals

Hi Xavier,

since you do not provide the info which tool you are using, nor
show your schema or the exact error message, I can just guess.
Generally, sum() is applicable to a list of numbers or to a list
of nodes that are each atomizable to a number. So, if your schema
defines the content of t1 to be of type string, you get a
run-time type error. In this case use xs:decimal() with xs bound
to “XML Schema”, to convert.

Regards,
Juliane.

Thank you Juliane,

t1 is defined as float. That’s what I don’t understand the problem. I’m using Tamino Interactive Interface on Tamino 4.1.4.4

The error message I get is :
INOXQE6301">Runtime type exception

thank you.

Hi Xavier,

yes, there is a bug in Tamino 4.1.4.4 concerning atomization,
i.e. the following query:

declare namespace xs = “XML Schema
let $list := ({xs:float(“1.2”)},{xs:float(“1.3”)})
return sum($list)

should work but throws 6301 as you mentioned.
There are two possible workarounds. First, upgrade to Tamino
4.2 where this is fixed, or use xs:float() to convert.
The following works in both Tamino versions:

declare namespace xs = “XML Schema
let $list := ({xs:float(“1.2”)},{xs:float(“1.3”)})
return sum(for $x in $list return xs:float($x))

Regards,
Juliane.