I’ve got a big XML file containing data concerning a Hotel chain. Per week, per day, per hotel they keep some data for their report:
<Report week="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="report.xsd">
<Days>
<Day id="1" naam="Monday">
<Hotels>
<Hotel id="1" naam="Bonotel Antwerp">
<Stays>
<Stay id="1">
<Room id="1" number="1"/>
<Roomtype id="1" naam="oneperson" price="20.00"/>
<Period id="1" naam="lowseason" price="20.00"/>
<Formula id="1" naam="roomandbreakfast" price="20.00"/>
<Facilities>
<Facility id="1" naam="swimming" price="5.00"/>
<Facility id="2" naam="golf" price="20.00"/>
</Facilities>
<Guest id="1" naam="John Williams"/>
</Stay>
<Stay id="2">
<Room id="2" number="2"/>
<Roomtype id="1" naam="oneperson" price="20.00"/>
<Period id="1" naam="lowseason" price="20.00"/>
<Formula id="1" naam="roomandbreakfast" price="20.00"/>
<Facilities>
<Facility id="2" naam="golf" price="20.00"/>
<Facility id="3" naam="minibar" price="10.00"/>
</Facilities>
<Guest id="2" naam="Ray Kurzweil"/>
</Stay>
<Stay id="3">
<Room id="3" number="3"/>
<Roomtype id="2" naam="twoperson" price="40.00"/>
<Period id="1" naam="lowseason" price="20.00"/>
<Formula id="2" naam="halfpension" price="30.00"/>
<Facilities>
<Facility id="4" naam="tennis" price="20.00"/>
<Facility id="4" naam="tennis" price="20.00"/>
</Facilities>
<Guest id="3" naam="Stephen Hawking"/>
</Stay>
</Stays>
</Hotel>
(: ... Other Hotels ... :)
</Hotels>
</Day>
(: ... Other Days ... :)
</Days>
</Report>
Using XQuery, I have to calculate what the average guests spends. This is the sum of the Roomtype price, the Periode price, the Formula price and the sum of the facilities. I came up with this XQuery:
xquery version "1.0";
<ReportResult week="1">
{
for $x in (1 to 7)
return
for $stays in doc("report.xml")//Report/Days/Day[@id=$x]/Hotels/Hotel[@id=1]/Stays
let $average := avg(
for $v in $stays/Stay
return sum($v/Roomtype/@price) + sum($v/Facilities/Facility/@prijs) + sum($v/Formula/@price) + sum($v/Period/@price)
)
return
<AverageSpending hotel="Bonotel Antwerpen" day="{data($x)}">
{data(round-half-to-even($average, 2))}
</AverageSpending>
}
</Reportresult>
This produces the result I expect:
<Reportresult week="1">
<Averagespending hotel="Bonotel Antwerpen" day="1">101.67</AverageSpending>
<Averagespending hotel="Bonotel Antwerpen" day="2">321.67</AverageSpending>
(: ... etc... :)
<Averagespending hotel="Bonotel Antwerpen" day="2">255</AverageSpending>
</Reportresult>
However, I also would like to calculate the total average. So the sum of all averages divided by the 7 days to output something like
<TotalAverage>198,67</TotalAverage>
But I’m having trouble calculating this Total Average. In Java I would use a total variable and increment it with the calculated average each loop but obviously that doesn’t work here. How would I be able to do this with XQuery? Thanks.
By example you can assign to a variable the first part of your request and calculate the total average on it and concat the both results :