Datypic Logo
XQuery

XQuery

Priscilla Walmsley (pwalmsley@datypic.com)

ISBN: 1491915102

2nd edition, 2015, O'Reilly Media, Inc.

Chapter 28: Additional XQuery-Related Standards

Please note that the book contains many inline examples and informal tables that are not provided here.

Example 28-1. Full-text query example
for $b in /books/book
let score $s := $b/content contains text ("web site" weight {0.5})
                           ftand ("usability" weight {2})
where $s > 0.5 
order by $s descending
return <result score="{$s}">{$b}</result>
Example 28-2. Simple FLWOR expression
for $prod in doc("catalog.xml")//product
order by $prod/name
return $prod/number
Example 28-3. Partial XQueryX equivalent of
<?xml version="1.0"?>
<xqx:module xmlns:xqx="http://www.w3.org/2005/XQueryX"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <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>  <!-- ... -->
Example 28-4. Using RESTXQ to retrieve a resource
xquery version "3.0";
module namespace prod = "http://datypic.com/prod";
declare namespace rest = "http://exquery.org/ns/restxq";
declare
  %rest:GET
  %rest:path("/product/{$num}")
  function prod:product-info($num) {
    let $prod := doc("catalog.xml")//product[number=$num]
    return
     <html>
       <body>
         <p><b>Number</b>: {data($prod/number)}</p>
         <p><b>Name</b>: {data($prod/name)}</p>
         <p><b>Department</b>: {data($prod/@dept)}</p>
       </body>
     </html>
};
Example 28-5. Passing parameters via RESTXQ
xquery version "3.0";
module namespace prod = "http://datypic.com/prod";
declare namespace rest = "http://exquery.org/ns/restxq";
declare
  %rest:GET
  %rest:path("/products")
  %rest:query-param("dept", "{$dept}", "*")
  function prod:prods-by-dept($dept) {
    <html>
      <body>{
        for $prod in doc("catalog.xml")//product[@dept=$dept or $dept = '*']
        let $num := data($prod/number)
        return <p><a href="product/{$num}">{$num}</a></p>
   }</body>
</html>
};
Example 28-6. Using RESTXQ to return an HTTP header
xquery version "3.0";
module namespace prod = "http://datypic.com/prod";
declare namespace rest = "http://exquery.org/ns/restxq";
declare namespace http = "http://expath.org/ns/http-client";
declare
  %rest:GET
  %rest:HEAD
  %rest:POST
  %rest:PUT
  %rest:DELETE
  function prod:not-found() {

    <rest:response>
      <http:response status="404" message="I was not found.">
        <http:header name="Content-Language" value="en"/>
        <http:header name="Content-Type" value="text/html; charset=utf-8"/>
      </http:response>
    </rest:response>
  };
Example 28-7. 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);
}
Datypic XQuery Services