In the document "prices.xml", find the minimum price for each book, in the
form of a minprice
element with the book title as its
title
attribute.
<results>
{
let $doc := doc("prices.xml")
for $t in distinct-values($doc/prices/book/title)
let $p := $doc/prices/book[title = $t]/price
return
<minprice title="{$t}">
{ min($p) }
</minprice>
}
</results>
Adapted from XML Query Use Cases