# XML - Managing Data Exchange/XQuery

## **1. Definition of XQuery**

XQuery is a query language under development by the World Wide Web
Consortium (W3C) and makes possible to efficiently and easily extract
information from native XML databases and relational databases that
store XML data.

Every query consists of an introduction and a body. The introduction
establishes the compile-time environment such as schema and module
imports, namespace and function declarations, and user-defined
functions. The body generates the value of the entire query. The
structure of XQuery shows in Figure 1.

```{=html}
<table class=MsoTableGrid border=0 cellspacing=0 cellpadding=0
 style='margin-left:5.4pt;border-collapse:collapse;mso-yfti-tbllook:480;
 mso-padding-alt:0in 5.4pt 0in 5.4pt;mso-border-insidev:.5pt solid windowtext'>
```
```{=html}
<tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>
```
```{=html}
<td width=624 colspan=3 style='width:6.5in;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}Figure 1.
Structure of `<span
  class=SpellE>`{=html}XQuery`</span>`{=html}`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:1'>
```
```{=html}
<td width=66 rowspan=8 style='width:49.5pt;border:none;border-right:solid windowtext 1.0pt;
  mso-border-right-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-fareast-language:KO;mso-bidi-language:#0400'>`{=html}Introduction`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}Comment:`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}(:
Sample version 1.0

` :)``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:2'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}Namespace
Declaration:`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}declare
namespace my =

` “``<span class=SpellE>`{=html}`urn:foo``</span>`{=html}`”;``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:3'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}Function
Declaration:`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}declare
function `<span
  class=SpellE>`{=html}my:fact`</span>`{=html}(\$n) {`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:4'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html} `</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html}if (\$n \< 2)`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:5'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html} `</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html}then 1`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:6'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html} `</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html}else \$n \*
`<span class=SpellE>`{=html}my:fact`</span>`{=html}(\$n --
1)`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:7'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html} `</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}};`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:8'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}Global
Variable:`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}declare
variable \$`<span
  class=SpellE>`{=html}my:ten`</span>`{=html}
{`<span class=SpellE>`{=html}my:fact`</span>`{=html}(10)};`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:9'>
```
```{=html}
<td width=624 colspan=3 style='width:6.5in;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html} `</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:10'>
```
```{=html}
<td width=66 rowspan=7 style='width:49.5pt;border:none;border-right:solid windowtext 1.0pt;
  mso-border-right-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}Body`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}Constructed
XML:`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}\<table\>{`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:11'>
```
```{=html}
<td width=162 rowspan=2 style='width:121.5pt;border:none;mso-border-left-alt:
  solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}FLWOR
Expression:`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html}for \$`<span class=SpellE>`{=html}i`</span>`{=html} in
1 to 10`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:12'>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html}return`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:13'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html} `</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}\<`<span class=SpellE>`{=html}tr`</span>`{=html}\>`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:14'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}Enclosed
Expression:`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html}\<`<span class=GramE>`{=html}td\>`</span>`{=html}10!/{\$`<span class=SpellE>`{=html}i`</span>`{=html}}!

` = {$``<span class=SpellE>`{=html}`my:ten``</span>`{=html}` div ``<span class=SpellE>`{=html}`my:fact``</span>`{=html}`($``<span
  class=SpellE>`{=html}`i``</span>`{=html}`)} </td>``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:15'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html} `</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}\</`<span
  class=SpellE>`{=html}tr`</span>`{=html}\>`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:16;mso-yfti-lastrow:yes'>
```
```{=html}
<td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html} `</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}}
\</table\>`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
</table>
```
## **2. XQuery versus Other Query Languages**

**2.1 XQuery versus XPath and XSLT**

XQuery, XPath, XSLT, and SQL are good query languages. Each of these
languages has their own advantages in diverse situations, so XQuery
cannot substitute for them at every task. XQuery is built on XPath
expressions. XQuery 1.0 and XPath 2.0 shares the same data model, the
same functions, and the same syntax. Table 1 shows the advantages and
the drawbacks of each query language.

Table 1. XQuery versus XPath and XSLT

```{=html}
<table class=MsoTableGrid border=1 cellspacing=0 cellpadding=0
 style='margin-left:5.4pt;border-collapse:collapse;border:none;mso-border-alt:
 solid windowtext .5pt;mso-yfti-tbllook:480;mso-padding-alt:0in 5.4pt 0in 5.4pt;
 mso-border-insideh:.5pt solid windowtext;mso-border-insidev:.5pt solid windowtext'>
```
```{=html}
<tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes;height:22.9pt'>
```
```{=html}
<td width=84 style='width:63.0pt;border:solid windowtext 1.0pt;mso-border-alt:
  solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt;height:22.9pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html} `</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=270 style='width:202.5pt;border:solid windowtext 1.0pt;border-left:
  none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt;height:22.9pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Advantage`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=270 style='width:202.5pt;border:solid windowtext 1.0pt;border-left:
  none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt;height:22.9pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Drawback`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:1'>
```
```{=html}
<td width=84 style='width:63.0pt;border:solid windowtext 1.0pt;border-top:
  none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span class=SpellE>`{=html}`<span lang=EN-US
  style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}XQuery`</span>`{=html}`</span>`{=html}`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html}`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=270 valign=top style='width:202.5pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}1.expressing
joins and

` sorts``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}2.manipulating

` sequences of values and nodes in arbitrary order``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}3.easy
to write

` user-defined functions including recursive ones``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}4.allows
users to

` construct temporary XML results in the middle of a query, and then navigate`\
` into that``</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=270 valign=top style='width:202.5pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}1.XQuery
implementations

` are less mature than XSLT ones``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:2'>
```
```{=html}
<td width=84 style='width:63.0pt;border:solid windowtext 1.0pt;border-top:
  none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span class=SpellE>`{=html}`<span lang=EN-US
  style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}XPath`</span>`{=html}`</span>`{=html}`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html} 1.0`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=270 valign=top style='width:202.5pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}1.convenient
syntax for

` addressing parts of an XML document``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}2.selecting
a node out

` of an existing XML document or database``</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=270 valign=top style='width:202.5pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}1.cannot
create new XML`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}2.cannot
select only

` part of an XML node``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}3.cannot
introduce

` variables or namespace bindings``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}4.cannot
work with date

` values, calculate the maximum of a set of numbers, or sort a list of strings``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:3;mso-yfti-lastrow:yes'>
```
```{=html}
<td width=84 style='width:63.0pt;border:solid windowtext 1.0pt;border-top:
  none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}XSLT
1.0`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=270 valign=top style='width:202.5pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}1.recursively

` processing an XML document or translating XML into HTML and text``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}2.creating
new XML or

` part of existing nodes``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}3.introducing
variables

` and namespaces``</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=270 valign=top style='width:202.5pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}1.cannot
be addressed

` without effectively creating a language like ``<span class=SpellE>`{=html}`XQuery``</span>`{=html}`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}2.cannot
work with

` sequences of values``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
</table>
```
**2.2 XQuery versus SQL**

XQuery has similarities to SQL in both style and syntax. The main
difference between XQuery and SQL is that SQL focuses on unordered sets
of "flat" rows, while XQuery focuses on ordered sequences of values and
hierarchical nodes.

## **3. XQuery Expressions**

**3.1 FLWOR expressions**

FLWOR expressions are important part of XQuery. FLWOR is pronounced
\"flower\". This name comes from the **F**OR, **L**ET, **W**HERE,
**O**RDER BY, and **R**ETURN clauses that organize the expressions. The
FOR and LET clauses can come out any number of times in any order. The
WHERE and ORDER BY clauses are optional. However, these clauses must be
shown in the order given if they are used. The RETURN clause should
exist.

XQuery permits you to use join queries in a similar way to SQL. This
example is depicted in Example 1 as a join between the videos table and
the actors table.

Example 1.

` let $doc := .`\
`   for $v in $doc//video,`\
`       $a in $doc//actors/actor`\
`     where ends-with($a, 'Lisa')`\
`         and $v/actorRef = $a/@id`\
`   order by $v/year`\
` return $v/title `

The **LET** clause states a variable assignment. In this case, the query
initializes it to doc (\'videos.xml\'), or a query's result places a
document in a database. The **FOR** clause describes a mechanism for
iteration: one variable processes all the videos in turn, another
variable processes all the actors in turn. In this case, the query
processes the pairs of videos and actors. The **WHERE** clause selects
tables in which you are interested. In this case, you want to know that
the actor shows in video table with the name ending with "Lisa". The
**ORDER BY** clause obtains the results in sorted order. In this case,
you desire to have a result with the videos in order of their release
date. The **RETURN** clause at the end of an expression informs the
system what information you want to get back. In this case, you want the
video's title.

------------------------------------------------------------------------

**3.2 Conditional expression**

XQuery offers IF, THEN, and ELSE clause, conditional expression. The
ELSE clause is obligatory. The reason is that each expression in XQuery
should return a value. A query is showed at example 2 to retrieve all
books and their authors. You desire to return additional authors as
"et-al" after the first two authors.

Example 2.

` for $b in document("books.xml")/bib/book`\
`   return`\
`     if (count($b/author) <= 2) then $b`\
`       else ``<book>`{=html}` { $b/@*, $b/title, $b/author[position() <= 2], ``<et-al/>`{=html}`,`\
`         ...... $b/publisher, $b/price } ``</book>`{=html}

This query reads book data from a books.xml. If the author count is less
than 2 or equal to 2 for each book, then the query returns the book
straightly. Otherwise the query makes a new book element including all
the original data, excepting that the query contains only the first two
authors and attaches an et-al element. Position() function is returned
only the first two authors. \$b/@\*, XPath expression, refers to all the
attributes on \$b.

------------------------------------------------------------------------

**3.3 XQuery functions and operators**

XQuery contains a huge set of functions and operators. Table 2 shows
frequently used built-in functions. You are able to describe your own
and many engines provide custom extensions as well.

Table 2. Commonly used built-in functions

```{=html}
<table class=MsoTableGrid border=1 cellspacing=0 cellpadding=0
 style='margin-left:5.4pt;border-collapse:collapse;border:none;mso-border-alt:
 solid windowtext .5pt;mso-yfti-tbllook:480;mso-padding-alt:0in 5.4pt 0in 5.4pt;
 mso-border-insideh:.5pt solid windowtext;mso-border-insidev:.5pt solid windowtext'>
```
```{=html}
<tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'>
```
```{=html}
<td width=228 valign=top style='width:171.0pt;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Function`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 valign=top style='width:297.0pt;border:solid windowtext 1.0pt;
  border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:
  solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Commentary`</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:1'>
```
```{=html}
<td width=228 valign=top style='width:171.0pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Math:`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}+, -, \*, div,
`<span class=SpellE>`{=html}idiv`</span>`{=html},

` mod, =, !=, <, >, <=, >= floor(), ceiling(), round(), count(),`\
` min(), max(), ``<span class=SpellE>`{=html}`avg``</span>`{=html}`(), sum()``</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 valign=top style='width:297.0pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Division is done using
div rather than a

` slash because a slash indicates an ``<span class=SpellE>`{=html}`XPath``</span>`{=html}` step`\
` expression. ``<span class=SpellE>`{=html}`<span class=GramE>`{=html}`idiv``</span>`{=html}`</span>`{=html}` is a`\
` special operator for integer-only division that returns an integer and`\
` ignores any remainder.``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:2'>
```
```{=html}
<td width=228 valign=top style='width:171.0pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Strings and Regular
Expressions:`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}compare(),
`<span class=SpellE>`{=html}concat`</span>`{=html}(),

` starts-with(), ends-with(), contains(), substring(), string-length(),`\
` substring-before(), substring-after(), normalize-space(), upper-case(),`\
` lower-case(), translate(), matches(), replace(), tokenize()``</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 valign=top style='width:297.0pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span class=GramE>`{=html}`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}compare(`</span>`{=html}`</span>`{=html}`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html}) dictates string ordering.
`<span class=GramE>`{=html}translate(`</span>`{=html})

` performs a special mapping of characters. ``<span class=GramE>`{=html}`matches(``</span>`{=html}`),`\
` replace(), and tokenize() use regular expressions to find, manipulate, and`\
` split string values.``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:3'>
```
```{=html}
<td width=228 valign=top style='width:171.0pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Date and
Time:`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}current-date(),
current-time(), current-`<span
  class=SpellE>`{=html}dateTime`</span>`{=html}() +, -, div
`<span class=SpellE>`{=html}eq`</span>`{=html}, `<span
  class=SpellE>`{=html}ne`</span>`{=html},
`<span class=SpellE>`{=html}lt`</span>`{=html},
`<span class=SpellE>`{=html}gt`</span>`{=html},

` le, ``<span class=SpellE>`{=html}`gt``</span>`{=html}`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 valign=top style='width:297.0pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span class=SpellE>`{=html}`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}XQuery`</span>`{=html}`</span>`{=html}`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html} has many special types for date and time values such
as duration, `<span
  class=SpellE>`{=html}dateTime`</span>`{=html}, date, and time. On most
you can do arithmetic

` and comparison operators as if they were numeric. The two-letter`\
` abbreviations stand for equal, not equal, less than, greater than, less than`\
` or equal, and greater than or equal.``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:4'>
```
```{=html}
<td width=228 valign=top style='width:171.0pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}XML node and
`<span class=SpellE>`{=html}QNames`</span>`{=html}:`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}node-kind(), node-name(),
base-`<span
  class=SpellE>`{=html}uri`</span>`{=html}()
`<span class=SpellE>`{=html}eq`</span>`{=html},
`<span class=SpellE>`{=html}ne`</span>`{=html},

` is, ``<span class=SpellE>`{=html}`isnot``</span>`{=html}`, get-local-name-from-``<span class=SpellE>`{=html}`QName``</span>`{=html}`(),`\
` get-namespace-from-``<span class=SpellE>`{=html}`QName``</span>`{=html}`() deep-equal() >>,`\
` <<``</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 valign=top style='width:297.0pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}node-`<span class=GramE>`{=html}kind(`</span>`{=html})

` returns the type of a node (i.e. "element"). node-``<span
  class=GramE>`{=html}`name(``</span>`{=html}`) returns the ``<span class=SpellE>`{=html}`QName``</span>`{=html}` of the`\
` node, if it exists. base-``<span class=SpellE>`{=html}`<span class=GramE>`{=html}`uri``</span>`{=html}`</span>`{=html}`<span
  class=GramE>`{=html}`(``</span>`{=html}`) returns the URI this node is from. ``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Nodes and
`<span class=SpellE>`{=html}QName`</span>`{=html}

` values can also be compared using ``<span class=SpellE>`{=html}`eq``</span>`{=html}` and ``<span
  class=SpellE>`{=html}`ne``</span>`{=html}` (for value comparison), or is and ``<span class=SpellE>`{=html}`isnot``</span>`{=html}\
` (for identity comparison). deep-``<span class=GramE>`{=html}`equal(``</span>`{=html}`) compares two`\
` nodes based on their full recursive content. ``</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}The \<\< operator returns
true if

` the left operand ``<span class=SpellE>`{=html}`preceeds``</span>`{=html}` the right operand in`\
` document order. The >> operator is a following comparison.``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:5'>
```
```{=html}
<td width=228 valign=top style='width:171.0pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Sequences:`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}item-at(), index-of(),
empty(),

` exists(), distinct-nodes(), distinct-values(), insert(), remove(),`\
` subsequence(), unordered().position(), last()``</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 valign=top style='width:297.0pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}item-`<span class=GramE>`{=html}at(`</span>`{=html})
returns

` an item at a given position while index-of() attempts to find a position for`\
` a given item. ``<span class=GramE>`{=html}`empty(``</span>`{=html}`) returns true if the sequence`\
` is empty and exists() returns true if it's not. ``<span class=SpellE>`{=html}`dictinct``</span>`{=html}`-``<span
  class=GramE>`{=html}`nodes(``</span>`{=html}`) returns a sequence with exactly identical nodes`\
` removed and distinct-values() returns a sequence with any duplicate atomic`\
` values removed. ``<span class=GramE>`{=html}`unordered(``</span>`{=html}`) allows the query engine`\
` to optimize without preserving order. ``<span class=GramE>`{=html}`position(``</span>`{=html}`)`\
` returns the position of the context item currently being processed. ``<span
  class=GramE>`{=html}`last(``</span>`{=html}`) returns the index of the last item.``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:6'>
```
```{=html}
<td width=228 valign=top style='width:171.0pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Type
Conversion:`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}string(), data(),
decimal(), `<span
  class=SpellE>`{=html}boolean`</span>`{=html}()`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 valign=top style='width:297.0pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}These functions return
the node as the

` given type, where possible. ``<span class=GramE>`{=html}`data(``</span>`{=html}`) returns the`\
` "typed value" of the node.``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:7'>
```
```{=html}
<td width=228 valign=top style='width:171.0pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Booleans:`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}true(), false(),
not()`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 valign=top style='width:297.0pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}There\'s no \"true\" or

` "false" keywords in ``<span class=SpellE>`{=html}`XQuery``</span>`{=html}` but rather ``<span
  class=GramE>`{=html}`true(``</span>`{=html}`) and false() functions. ``<span class=GramE>`{=html}`not(``</span>`{=html}`)`\
` returns the ``<span class=SpellE>`{=html}`boolean``</span>`{=html}` negation of its argument.``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
<tr style='mso-yfti-irow:8;mso-yfti-lastrow:yes'>
```
```{=html}
<td width=228 valign=top style='width:171.0pt;border:solid windowtext 1.0pt;
  border-top:none;mso-border-top-alt:solid windowtext .5pt;mso-border-alt:solid windowtext .5pt;
  padding:0in 5.4pt 0in 5.4pt'>
```
`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}Input:`</span>`{=html}

`<span lang=EN-US style='font-size:10.0pt;line-height:150%;
  color:black;mso-bidi-language:#0400'>`{=html}document(), input(),
collection()`</span>`{=html}

```{=html}
</td>
```
```{=html}
<td width=396 valign=top style='width:297.0pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt'>
```
`<span class=GramE>`{=html}`<span lang=EN-US style='font-size:10.0pt;
  line-height:150%;color:black;mso-bidi-language:#0400'>`{=html}document(`</span>`{=html}`</span>`{=html}`<span
  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:
  #0400'>`{=html}) returns a document of nodes based on a URI parameter.
`<span
  class=GramE>`{=html}collection(`</span>`{=html}) returns a collection
based on a string

` parameter (perhaps multiple documents). ``<span class=GramE>`{=html}`input(``</span>`{=html}`)`\
` returns s general engine-provided set of input nodes.``</span>`{=html}

```{=html}
</td>
```
```{=html}
</tr>
```
```{=html}
</table>
```
## **4. References**

The contents of this chapter were quoted from the following lists.

\- *X Is for XQuery*, Jason Hunter:
<http://www.oracle.com/technology/oramag/oracle/03-may/o33devxml.html>

\- *An Introduction to the XQuery FLWOR Expression*, Michael Kay:
<http://www.stylusstudio.com/xquery_flwor.html>

\- *Learn XQuery in 10 Minutes*, Michael Kay:
<http://www.stylusstudio.com/xquery_primer.html>

\- *XQuery: The XML Query Language*, Michael Brundage, Addison-Wesley
2004

## **5. Useful Links and Books**

\- W3C XML Query (XQuery): <http://www.w3.org/XML/Query>

\- XQuery Latest version: <http://www.w3.org/TR/xquery/>

\- XQuery 1.0 and XPath 2.0 Functions and Operators:
<http://www.w3.org/TR/xpath-functions/>

\- XQuery 1.0 and XPath 2.0 Data Model (XDM):
<http://www.w3.org/TR/xpath-datamodel/>

\- XSLT 2.0 and XQuery 1.0 Serialization:
<http://www.w3.org/TR/xslt-xquery-serialization/>

\- XML Query Use Cases: <http://www.w3.org/TR/xquery-use-cases/>

\- XML Query (XQuery) Requirements:
<http://www.w3.org/TR/xquery-requirements/>

\- XQuery: The XML Query Language, Michael Brundage, Addison-Wesley 2004

## See Also

-   XQuery Tutorial and Cookbook Wikibook This
    Wikibook has many small XQuery examples with links to working XQuery
    applications.

