I guess the example XQuery at the end: for $pn in fn:distinct-values( fn:doc("catalog.xml")/items/item/partno) let $i := fn:doc("catalog.xml")/items/item[partno = $pn] where fn:count($i) >= 3 order by $pn return <well-supplied-item> <partno> {$p} </partno> <avgprice> {fn:avg($i/price)} </avgprice> </well-supplied-item> should be: for $pn in fn:distinct-values( fn:doc("catalog.xml")/items/item/partno) let $i := fn:doc("catalog.xml")/items/item[partno = $pn] where fn:count($i) >= 3 order by $pn return <well-supplied-item> <partno> {$pn} </partno> <avgprice> {fn:avg($i/price)} </avgprice> </well-supplied-item>
You're right, the example refers to the variable $p, which is not bound: <partno> {$p} </partno> You have corrected this to use $pn: <partno> {$pn} </partno> (I'm saying this to make it faster for others to spot!)