Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:44:34+00:00 2026-06-09T17:44:34+00:00

I’ve got a big XML file containing data concerning a Hotel chain. Per week,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:44:36+00:00Added an answer on June 9, 2026 at 5:44 pm

    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 :

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.