Using XQuery Expressions in Construction Statements
XQuery is another query syntax for XML, like XPath, that uses a syntax similar to SQL. XPath expressions are also valid XQuery expressions, but XQuery includes some additional features to handle more complex queries.
You can use XQuery expressions as
Dynamic Mashup Expressions inside literal XML for the <constructor> and <appendresult> statements. All of the variables and input parameters for the mashup script are available within XQuery, allowing you to manipulate the results from mashable information sources.
Some tasks that XQuery is useful for include queries that iterate through several mashables results, queries that should be sorted before constructing results or queries that require complicated calculations. For more information, try the simple XQuery tutorial at
http://www.stylusstudio.com/xquery_primer.html or see the
XQuery Specification.
FLWOR Syntax
The basic syntax for XQuery is known as FLWOR:
for: to iterate through variable data.
let: to define new variables, either constructed as XML or using functions to other XQuery or XPath expressions to calculate values.
where: to specify the criteria used to select data. This can also act like a join.
order: to sort the selected, calculated and joined results.
return: to construct the actual XML to return.
The example shown here illustrates the basic syntax of all for the FLWOR statements:
<constructor outputvariable="$result" >
<res:result>
{
for $i in distinct-values($demographics//zip-code),
$j in distinct-values($weblogs//zip-code)
let $purchases := $weblogs/weblog/record[ zip-code = $j],
$total-purchases := sum(data($purchases//purchase-amount)),
$population := $demographics/demographics/person[zip-code = $i],
$income-avg := avg(data($population//income)),
where $i = $j
order by $total-purchases descending
return <res:item
zip-code="{$i}"
total-purchase="{$total-purchases}"
income-average="{$income-avg}" />
}
</res:result>
</constructor>