qexo-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qexo-general] problems aggregating with sum()


From: Kevin Y. Kim (Lists)
Subject: [Qexo-general] problems aggregating with sum()
Date: Wed, 26 Oct 2005 21:29:59 -0400

Hi,

(I am new to XQuery and Qexo. I've searched the web for an answer to no avail.)

I want to transform an XML file, aggregating on certain nodes.

If I have an XML file, input.xml, that looks like this:
<record id="1">
    <key>A</key>
    <value>1</value>
</record>
<record id="2">
    <key>B</key>
    <value>2</value>
</record>
<record id="3">
    <key>C</key>
    <value>3</value>
</record>
<record id="4">
    <key>A</key>
    <value>4</value>
</record>
<record id="5">
    <key>B</key>
    <value>5</value>
</record>

I want the output to look like this:
<record>
    <key>A</key>
    <value>5</value>
</record>
<record>
    <key>B</key>
    <value>7</value>
</record>
<record>
    <key>C</key>
    <value>3</value>
</record>


My XQuery looks like this:
declare function local:records-for-key($doc, $key)
{
  for $rec in $doc/record[key = $key]
  return $rec
};

let $doc := doc("input.xml")
for $key in distinct-values($doc/record/key)
let $recs := local:records-for-key($doc, $key)
return
<record>
  <key>{$key}</key>
  <value>{sum($recs/value/text())}</value>
</record>

When I try this xquery with Qexo, I get an exception:
record><key>A</key><valueInvalid parameter, was: null
java.lang.ClassCastException
        at gnu.kawa.functions.Arithmetic.asNumeric(Arithmetic.java:169)
        at gnu.kawa.functions.AddOp.apply2(AddOp.java:76)
        at gnu.kawa.functions.AddOp.applyN(AddOp.java:118)
        at gnu.kawa.functions.AddOp.applyN(AddOp.java:133)
        at gnu.mapping.ProcedureN.apply2(ProcedureN.java:39)
        at gnu.xquery.util.Reduce.combine(Reduce.java:26)
        at gnu.xquery.util.Reduce.apply1(Reduce.java:42)
        at atInteractiveLevel$2.run(test.xq:13)
        at gnu.expr.ModuleExp.evalModule(ModuleExp.java:215)
        at kawa.Shell.run(Shell.java:229)
        at kawa.standard.load.loadSource(load.java:149)
        at kawa.standard.load.apply(load.java:267)
        at kawa.Shell.runFile(Shell.java:299)
        at kawa.repl.processArgs(repl.java:249)
        at kawa.repl.main(repl.java:728)

As far as I can tell, the problem is the SUM() function.
Just on a lark, I changed the SUM() to COUNT() - and it does
report the correct number of items in the sequence.

What am I doing wrong?

Thanks,
-kevin
p.s. sorry for the length....





reply via email to

[Prev in Thread] Current Thread [Next in Thread]