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

  • Home
  • SEARCH
  • 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 8662061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:38:42+00:00 2026-06-12T16:38:42+00:00

I have a xml file like below, and i wanted to group the toyID

  • 0

I have a xml file like below, and i wanted to group the toyID and get the sum of how much that toy has been sold. and order it by the toyID with the highest sales.but the code that i wrote is able to sum the amount of toy sold,but the order by the highest amount of toy sold is not working.can i know which part i did wrongly?

 ToySale.XML
 <toySale companyID="1" toyID="11" >
   <amount>15</amount>
 </toySale>
 <toySale companyID="3" toyID="11" >
   <amount>12</amount>
 </toySale>
 <toySale companyID="1" toyID="22" >
   <amount>3</amount>
 </toySale>
 <toySale companyID="2" toyID="33" >
   <amount>7</amount>
 </toySale>

below is the code that i wrote:

for $t in distinct-values(ToySale.xml")//@toyID)
return <toyID>
            toyID : {$t} 

          <totalSale>{sum(for $sum in (ToySale.xml")//toySale
                     order by $sum/@toyID/../amount     
                     where $t=$sum/@toyID
                     return $sum/@productID/../amount)  }

           </totalSale

        </toyID>
  • 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-12T16:38:43+00:00Added an answer on June 12, 2026 at 4:38 pm

    This query:

       for $vId
         in (/*/toySale/@toyID)
                         [index-of(/*/toySale/@toyID, .)[1]]
    
        let $vSales := /*/toySale[@toyID eq $vId]
    
        order by sum($vSales/amount) descending
    
       return
         <sale toy-id="{$vId}" total="{sum($vSales/amount)}"/>
    

    When applied on the provided XML document:

    <toySales>
        <toySale companyID="1" toyID="11" >
            <amount>15</amount>
        </toySale>
        <toySale companyID="3" toyID="11" >
            <amount>12</amount>
        </toySale>
        <toySale companyID="1" toyID="22" >
            <amount>3</amount>
        </toySale>
        <toySale companyID="2" toyID="33" >
            <amount>7</amount>
        </toySale>
    </toySales>
    

    produces the wanted, correct result:

    <sale total="27" toy-id="11"/>
    <sale total="7" toy-id="33"/>
    <sale total="3" toy-id="22"/>
    

    An equivalent and slightly shorter query:

       for $vId in distinct-values(/*/toySale/@toyID)
    
         let $vSales := /*/toySale[@toyID eq $vId]
    
         order by sum($vSales/amount) descending
    
        return
           <sale toy-id="{$vId}" total="{sum($vSales/amount)}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file which has many section like the one below: <Operations>
I have an xml file like down below. I want to get pharmacies nodes'
i have below 2 xml file(fruit.xml and toy.xml).I wanted to use xquery to retrieve
i have xml file like Below and Device class and i want get List<Device>
I have a sample xml file which looks like the one below: <Root> <SubOne>
I have an XML file that looks like <?xml version=1.0> <playlist> <name>My Playlist</name> <song>
I have an XML file that reads like this <abc> <ab>value</ab> <aa>time</aa> <ac>money</ac> </abc>
I have an XML file like below , using xpath and xnavigator how can
We have XML file like below... <?xml version='1.0'?> <T0020 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://www.safersys.org/namespaces/T0020V1 T0020V1.xsd xmlns=http://www.safersys.org/namespaces/T0020V1> <IRP_ACCOUNT>
I have a XML file which contains a part like below. img and br

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.