(Q10) List each publisher and the average price of its books.

FOR $p IN distinct(document("bib.xml")//publisher)
LET $a := avg(document("bib.xml")
   /book[publisher = $p]/price)
RETURN 
   <publisher>
      <name> $p/text() </name> ,
      <avgprice> $a </avgprice>
   </publisher>
  • The distinct() function returns an unordered set of nodes, one for each unique value found among those nodes (the grouping problem)
  • Current XSLT solutions to this problem are clumsy; XSLT 2.0 promises to fix this
  • The avg() function is among the additional functions provided by the XQuery path expression language. It provides some convenience over using only XPath's sum() and count() functions.
  • LET binds a variable in a similar way to xsl:variable
<<<  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15    >>>