Chapter 22: Additional XQuery-Related Standards
Example 22-1. Simple FLWOR
for $product in doc("catalog.xml")//product
order by $product/name
return $product/number
Example 22-2. Partial XQueryX equivalent of Example 22-1
<?xml version="1.0"?>
<xqx:module xmlns:xqx="http://www.w3.org/2005/XQueryX"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2005/XQueryX
http://www.w3.org/2005/XQueryX/xqueryx.xsd">
<xqx:mainModule>
<xqx:queryBody>
<xqx:flworExpr>
<xqx:forClause>
<xqx:forClauseItem>
<xqx:typedVariableBinding>
<xqx:varName>product</xqx:varName>
</xqx:typedVariableBinding>
<xqx:forExpr>
<xqx:pathExpr>
<xqx:argExpr>
<xqx:functionCallExpr>
<xqx:functionName>doc</xqx:functionName>
<xqx:arguments>
<xqx:stringConstantExpr>
<xqx:value>catalog.xml</xqx:value>
</xqx:stringConstantExpr>
</xqx:arguments>
</xqx:functionCallExpr>
</xqx:argExpr>
<xqx:stepExpr>
<xqx:xpathAxis>descendant-or-self</xqx:xpathAxis>
<xqx:anyKindTest/>
</xqx:stepExpr>
<xqx:stepExpr>
<xqx:xpathAxis>child</xqx:xpathAxis>
<xqx:nameTest>product</xqx:nameTest>
</xqx:stepExpr>
</xqx:pathExpr>
</xqx:forExpr>
</xqx:forClauseItem>
</xqx:forClause> <!-- ... -->
</xqx:flworExpr>
</xqx:queryBody>
</xqx:mainModule>
</xqx:module>
Example 22-3. Full-text query example
for $b score $s in /books/book[content ftcontains ("web site" weight 0.2)
&& ("usability" weight 0.8)]
where $s > 0.5
order by $s descending
return <result>
<title> {$b//title} </title>
<score> {$s} </score>
</result>
Example 22-4. XQJ example
// connect to the data source
XQConnection conn = xqds.getConnection();
// create a new expression object
XQExpression expr = conn.createExpression();
// execute the query
XQResultSequence result = expr.executeQuery(
"for $prod in doc('catalog.xml')//product" +
"order by $prod/number" +
"return $prod/name");
// iterate through the result sequence
while (result.next()) {
// retrieve the atomic value of the current item
String prodName = result.getAtomicValue();
System.out.println("Product name: " + prodName);
}
|