Calculate average using XQuery -
i need calculate average of each courses using xquery.
here's xml code :
<?xml version="1.0" encoding="iso-8859-1" ?> <?xml-stylesheet href="class.xsl" type="text/xsl" ?> <university> <student><sname>charlie parker</name> <course sigle="inf8430" note="69" /> <course sigle="inf1030" note="65" /> <course sigle="inf1230" note="73" /></student> <student><name>miles davis</name> <course sigle="inf8430" note="65" /> <course sigle="inf1030" note="77" /> <course sigle="inf1230" note="83" /></student> <student><name>john coltrane</name> <course sigle="inf9430" note="24" /> <course sigle="inf1030" note="64" /> <course sigle="inf1230" note="56" /></student> <student><name>charles mingus</name> <course sigle="inf8430" note="34" /> <course sigle="inf1230" note="89" /></student> </university>
the output file should :
<list> <course sigle="inf8430">85</course> <course sigle="inf1030">76.66666666666667</course> <course sigle="inf1230">81.5</course> <course sigle="inf9430">39</course> </list>
thanks lot help!
element list { let $courses := doc('mydoc.xml')/university/student/course $id in distinct-values($courses/@sigle) let $values := $courses[@sigle = $id]/xs:double(@note) return element course { attribute sigle { $id }, fn:avg($values) } }
Comments
Post a Comment