1

我有一个很大的 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 做到这一点?谢谢。

4

1 回答 1

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>
于 2012-08-13T18:44:36.120 回答