我有一个包含酒店连锁数据的大型XML文件。每周,每个酒店,每个酒店都会为他们的报告保留一些数据:
<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>
使用XQuery,我必须计算一般客人的花费。这是Roomtype价格,Periode价格,Formula价格和设施总和的总和。我想出了这个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>
这会产生我期望的结果:
<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>
但是,我也想计算总平均值。所以所有平均值之和除以7天输出类似
<TotalAverage>198,67</TotalAverage>
但我在计算此总平均值时遇到问题。在Java中,我将使用一个总变量并使用计算出的每个循环的平均值来增加它,但显然这在这里不起作用。我怎么能用XQuery做到这一点?感谢。
答案 0 :(得分:2)
例如,您可以为变量分配请求的第一部分,并计算其总平均值,并将两个结果连接起来:
xquery version "1.0";
<ReportResult week="1">
{ let $averages :=
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>
return ($averages,<TotalAverage>{avg($averages)}</TotalAverage>)
</Reportresult>