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.

   Figure 1. Structure of XQuery   Introduction  Comment:  (: Sample version 1.0 :) <tr style='mso-yfti-irow:2'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>Namespace Declaration: <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>declare namespace my = “ urn:foo ”; <tr style='mso-yfti-irow:3'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>Function Declaration: <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>declare function <span class=SpellE>my:fact ($n) { <tr style='mso-yfti-irow:4'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'> <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; text-indent:10.0pt;mso-char-indent-count:1.0;line-height:150%;mso-pagination: none;layout-grid-mode:char;text-autospace:none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language: #0400'>if ($n &lt; 2) <tr style='mso-yfti-irow:5'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'> <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; text-indent:20.0pt;mso-char-indent-count:2.0;line-height:150%;mso-pagination: none;layout-grid-mode:char;text-autospace:none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language: #0400'>then 1 <tr style='mso-yfti-irow:6'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'> <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; text-indent:10.0pt;mso-char-indent-count:1.0;line-height:150%;mso-pagination: none;layout-grid-mode:char;text-autospace:none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language: #0400'>else $n * my:fact ($n &#8211; 1) <tr style='mso-yfti-irow:7'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'> <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>}; <tr style='mso-yfti-irow:8'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>Global Variable: <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>declare variable $<span class=SpellE>my:ten { my:fact (10)}; <tr style='mso-yfti-irow:9'> <td width=624 colspan=3 style='width:6.5in;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'> <tr style='mso-yfti-irow:10'> <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>Body <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>Constructed XML: <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>&lt;table&gt;{ <tr style='mso-yfti-irow:11'> <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>FLWOR Expression: <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; text-indent:10.0pt;mso-char-indent-count:1.0;line-height:150%;mso-pagination: none;layout-grid-mode:char;text-autospace:none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language: #0400'>for $ i in 1 to 10 <tr style='mso-yfti-irow:12'> <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; text-indent:10.0pt;mso-char-indent-count:1.0;line-height:150%;mso-pagination: none;layout-grid-mode:char;text-autospace:none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language: #0400'>return <tr style='mso-yfti-irow:13'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'> <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>&lt; tr &gt; <tr style='mso-yfti-irow:14'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>Enclosed Expression: <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; text-indent:10.0pt;mso-char-indent-count:1.0;line-height:150%;mso-pagination: none;layout-grid-mode:char;text-autospace:none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language: #0400'>&lt; td&gt; 10!/{$ i }! = {$ my:ten div my:fact ($<span class=SpellE>i )} &lt;/td&gt; <tr style='mso-yfti-irow:15'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'> <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>&lt;/<span class=SpellE>tr &gt; <tr style='mso-yfti-irow:16;mso-yfti-lastrow:yes'> <td width=162 style='width:121.5pt;border:none;mso-border-left-alt:solid windowtext .5pt; padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'> <td width=396 style='width:297.0pt;padding:0in 5.4pt 0in 5.4pt'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>} &lt;/table&gt;

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 <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'> <tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes;height:22.9pt'> <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'> <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'> <p class=MsoNormal align=center style='text-align:center;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Advantage <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'> <p class=MsoNormal align=center style='text-align:center;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Drawback <tr style='mso-yfti-irow:1'> <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'> <span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:#0400'>XQuery <span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language: #0400'>   <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>1.expressing joins and sorts <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>2.manipulating sequences of values and nodes in arbitrary order <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>3.easy to write user-defined functions including recursive ones <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>4.allows users to construct temporary XML results in the middle of a query, and then navigate into that <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>1.XQuery implementations are less mature than XSLT ones <tr style='mso-yfti-irow:2'> <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'> <span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:#0400'>XPath <span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language: #0400'> 1.0   <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>1.convenient syntax for addressing parts of an XML document <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>2.selecting a node out of an existing XML document or database <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>1.cannot create new XML <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>2.cannot select only part of an XML node <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>3.cannot introduce variables or namespace bindings <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>4.cannot work with date values, calculate the maximum of a set of numbers, or sort a list of strings <tr style='mso-yfti-irow:3;mso-yfti-lastrow:yes'> <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>XSLT 1.0 <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>1.recursively processing an XML document or translating XML into HTML and text <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>2.creating new XML or part of existing nodes <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>3.introducing variables and namespaces <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'> <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>1.cannot be addressed without effectively creating a language like XQuery <p class=MsoNormal style='text-align:justify;text-justify:inter-ideograph; line-height:150%;mso-pagination:none;layout-grid-mode:char;text-autospace: none;word-break:keep-all'><span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>2.cannot work with sequences of values

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 FOR, LET, WHERE, ORDER BY, and RETURN 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 { $b/@*, $b/title, $b/author[position <= 2], <et-al/>, ...... $b/publisher, $b/price }

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 <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'> <tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes'> <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'> <p class=MsoNormal align=center style='text-align:center;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Function <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'> <p class=MsoNormal align=center style='text-align:center;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Commentary <tr style='mso-yfti-irow:1'> <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Math: <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>+, -, *, div, idiv , mod, =, !=, &lt;, &gt;, &lt;=, &gt;= floor, ceiling, round, count, min, max, avg, sum <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Division is done using div rather than a slash because a slash indicates an XPath step expression. idiv is a  special operator for integer-only division that returns an integer and ignores any remainder. <tr style='mso-yfti-irow:2'> <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Strings and Regular Expressions: <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>compare, concat , starts-with, ends-with, contains, substring, string-length, substring-before, substring-after, normalize-space, upper-case, lower-case, translate, matches, replace, tokenize <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'> <span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>compare( <span  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:  #0400'>) dictates string ordering. translate performs a special mapping of characters. matches, replace, and tokenize use regular expressions to find, manipulate, and split string values. <tr style='mso-yfti-irow:3'> <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Date and Time: <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>current-date, current-time, current-<span class=SpellE>dateTime +, -, div eq, <span class=SpellE>ne, lt , gt , le, gt    <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'> <span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>XQuery <span lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language: #0400'> has many special types for date and time values such as duration, <span class=SpellE>dateTime, 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. <tr style='mso-yfti-irow:4'> <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>XML node and QNames : <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>node-kind, node-name, base-<span class=SpellE>uri eq, ne , is, isnot, get-local-name-from- QName , get-namespace-from- QName deep-equal &gt;&gt;, &lt;&lt; <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>node- kind returns the type of a node (i.e. &quot;element&quot;). node-<span class=GramE>name returns the QName of the node, if it exists. base- uri  <span class=GramE> returns the URI this node is from. <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Nodes and QName values can also be compared using eq and <span class=SpellE>ne (for value comparison), or is and isnot (for identity comparison). deep- equal compares two nodes based on their full recursive content. <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>The &lt;&lt; operator returns true if the left operand preceeds the right operand in  document order. The &gt;&gt; operator is a following comparison. <tr style='mso-yfti-irow:5'> <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Sequences: <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>item-at, index-of, empty, exists, distinct-nodes, distinct-values, insert, remove, subsequence, unordered.position, last <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>item- at returns an item at a given position while index-of attempts to find a position for a given item. empty returns true if the sequence is empty and exists returns true if it's not. dictinct -<span class=GramE>nodes returns a sequence with exactly identical nodes removed and distinct-values returns a sequence with any duplicate atomic values removed. unordered allows the query engine to optimize without preserving order. position returns the position of the context item currently being processed. <span class=GramE>last returns the index of the last item. <tr style='mso-yfti-irow:6'> <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Type Conversion: <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>string, data, decimal, <span class=SpellE>boolean <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>These functions return the node as the given type, where possible. data returns the &quot;typed value&quot; of the node. <tr style='mso-yfti-irow:7'> <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Booleans: <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>true, false, not <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>There's no &quot;true&quot; or &quot;false&quot; keywords in XQuery but rather <span class=GramE>true and false functions. not returns the boolean negation of its argument. <tr style='mso-yfti-irow:8;mso-yfti-lastrow:yes'> <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>Input: <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'><span lang=EN-US style='font-size:10.0pt;line-height:150%; color:black;mso-bidi-language:#0400'>document, input, collection <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'> <p class=MsoNormal align=left style='text-align:left;line-height:150%; mso-pagination:none;layout-grid-mode:char;text-autospace:none;word-break: keep-all'> <span lang=EN-US style='font-size:10.0pt; line-height:150%;color:black;mso-bidi-language:#0400'>document( <span  lang=EN-US style='font-size:10.0pt;line-height:150%;color:black;mso-bidi-language:  #0400'>) returns a document of nodes based on a URI parameter. <span class=GramE>collection returns a collection based on a string parameter (perhaps multiple documents). input returns s general engine-provided set of input nodes.

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