(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>
XSLT equivalent to (Q10)
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="document('bib.xml')//publisher[not(.=preceding::publisher)]">
<xsl:variable name="prices"
select="document('bib.xml')/book[publisher=current()]/price"/>
<xsl:variable name="avgPrice" select="sum($prices) div count($prices)"/>
<publisher>
<name><xsl:value-of select="."/></name>
<avgprice><xsl:value-of select="$avgPrice"/></avgprice>
</publisher>
</xsl:for-each>
</xsl:template>
</xsl:transform>
|