ViRALiC
ViRALiC

Reputation: 1549

XQuery basics - how to keep track of multiple variables?

In XQuery, how would I go about keeping track of certain variables, and in this case, return the average of all such variables?

This is a small cutout of the database I'm experimenting with.

<?xml version="1.0" encoding="UTF-8"?>

<catalog> 
  <book id="bk101"> 
    <author>Gambardella, Matthew</author>  
    <title>XML Developer's Guide</title>  
    <genre>Computer</genre>  
    <price>44.95</price>  
    <publish_date>2000-10-01</publish_date>  
    <description>An in-depth look at creating applications with XML.</description> 
  </book>  
  <book id="bk102"> 
    <author>Ralls, Kim</author>  
    <title>Midnight Rain</title>  
    <genre>Fantasy</genre>  
    <price>5.95</price>  
    <publish_date>2000-12-16</publish_date>  
    <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> 
  </book>  
  <book id="bk103"> 
    <author>Corets, Eva</author>  
    <title>Maeve Ascendant</title>  
    <genre>Fantasy</genre>  
    <price>5.95</price>  
    <publish_date>2000-11-17</publish_date>  
    <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description> 
  </book> 
</catalog>

Now, what I am trying to do, or trying to understand how works, is obtaining the average price for all three books combined.

What I've tried looks like:

for $eachElement in catalog/book
where $eachElement/price > 0
return avg($eachElement/price)

What this does is return each price individually. As you can probably guess, this isn't what I want.

Could anyone explain to me how I could get the average price of all three books, and how XQuery completes this process?

Upvotes: 0

Views: 82

Answers (2)

dirkk
dirkk

Reputation: 6218

You did use a FLWOR expression, i.e. you are iterating over a sequence. . This sequence is generated by catalog/book, which holds all the book elements. When you now put this into a for expression, each book will be computed separated. Thus, you compute the average of exactly one element in each step - Which, of course, will always be the element itself.

Thus, you should pass the whole sequence to avg. You can filter out values using a predicate []:

avg(catalog/book[price > 0]/price)

Upvotes: 1

Ewout Graswinckel
Ewout Graswinckel

Reputation: 1273

you're now indeed 'iterating' through all items individually. The function avg(..) actually accepts a sequence of elements, so what you should do it select all price elements you want to average and pass it directly to the avg(..) function:

avg(catalog/book/price)

Upvotes: 1

Related Questions