XQuery

XQuery

(pwalmsley@datypic.com)

ISBN: 1491915103

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

Chapter 2: XQuery Foundations

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

Example 2-1. A query with a prolog
Query
xquery version "3.1";
            
declare namespace html = "http://www.w3.org/1999/xhtml";
declare variable $orderTitle := "Order Report";

<h1>{$orderTitle}</h1>,
for $item in doc("order.xml")//item
order by $item/@num
return <p>{data($item/@num)}</p>
Results
<h1>Order Report</h1>
<p>443</p>
<p>557</p>
<p>557</p>
<p>563</p>
<p>784</p>
<p>784</p>
Example 2-2. Small XML example
<catalog xmlns="http://datypic.com/cat">
  <product dept="MEN" xmlns="http://datypic.com/prod">
    <number>784</number>
    <name language="en">Cotton Dress Shirt</name>
    <colorChoices>white gray</colorChoices>
    <desc>Our <i>favorite</i> shirt!</desc>
  </product>
</catalog>
Example 2-3. Input document with namespaces (prod_ns.xml)
<prod:product xmlns:prod="http://datypic.com/prod">
  <prod:number>563</prod:number>
  <prod:name language="en">Floppy Sun Hat</prod:name>
</prod:product>
Example 2-4. Querying with namespaces
Query
declare namespace prod = "http://datypic.com/prod";
for $prod in doc("prod_ns.xml")/prod:product
return $prod/prod:name
Results
<prod:name xmlns:prod="http://datypic.com/prod"
           language="en">Floppy Sun Hat</prod:name>
Datypic XQuery Services