Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 9026c9fecbedd778b04292bd6f2d9049 > files > 107

qtxmlpatterns5-doc-5.9.4-1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- xquery-introduction.qdoc -->
  <title>A Short Path to XQuery | Qt XML Patterns 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td ><a href="qtxmlpatterns-index.html">Qt XML Patterns</a></td><td >A Short Path to XQuery</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="start" href="xmlprocessing.html" />
<p class="naviNextPrevious headerNavi">
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#introduction">Introduction</a></li>
<li class="level1"><a href="#using-path-expressions-to-match-and-select-items">Using Path Expressions To Match And Select Items</a></li>
<li class="level2"><a href="#axis-steps">Axis Steps</a></li>
<li class="level2"><a href="#axis-specifiers">Axis Specifiers</a></li>
<li class="level2"><a href="#node-tests">Node Tests</a></li>
<li class="level2"><a href="#shorthand-form">Shorthand Form</a></li>
<li class="level2"><a href="#name-tests">Name Tests</a></li>
<li class="level1"><a href="#using-predicates-in-path-expressions">Using Predicates In Path Expressions</a></li>
<li class="level2"><a href="#positional-predicates">Positional Predicates</a></li>
<li class="level2"><a href="#boolean-predicates">Boolean Predicates</a></li>
<li class="level1"><a href="#constructing-elements">Constructing Elements</a></li>
<li class="level2"><a href="#element-constructors-are-expressions">Element Constructors are Expressions</a></li>
<li class="level1"><a href="#constructing-atomic-values">Constructing Atomic Values</a></li>
<li class="level1"><a href="#running-the-cookbook-examples">Running The Cookbook Examples</a></li>
<li class="level1"><a href="#further-reading">Further Reading</a></li>
<li class="level1"><a href="#faq">FAQ</a></li>
<li class="level2"><a href="#why-didn-t-my-path-expression-match-anything">Why didn't my path expression match anything?</a></li>
<li class="level2"><a href="#what-if-my-input-namespace-is-different-from-my-output-namespace">What if my input namespace is different from my output namespace?</a></li>
<li class="level2"><a href="#why-doesn-t-my-return-clause-work">Why doesn't my return clause work?</a></li>
<li class="level2"><a href="#why-didn-t-my-expression-get-evaluated">Why didn't my expression get evaluated?</a></li>
<li class="level2"><a href="#my-predicate-is-correct-so-why-doesn-t-it-select-the-right-stuff">My predicate is correct, so why doesn't it select the right stuff?</a></li>
<li class="level2"><a href="#why-doesn-t-my-flwor-behave-as-expected">Why doesn't my FLWOR behave as expected?</a></li>
<li class="level2"><a href="#why-are-my-elements-created-in-the-wrong-order">Why are my elements created in the wrong order?</a></li>
<li class="level2"><a href="#why-can-t-i-use-keyword-true-keyword-and-keyword-false-keyword-in-my-xquery">Why can't I use <code>true</code> and <code>false</code> in my XQuery?</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">A Short Path to XQuery</h1>
<span class="subtitle"></span>
<!-- $$$xquery-introduction.html-description -->
<div class="descr"> <a name="details"></a>
<p><a href="xmlprocessing.html">XQuery</a> is a language for querying XML data or non-XML data that can be modeled as XML. <a href="xmlprocessing.html">XQuery</a> is specified by the <a href="http://www.w3.org">W3C</a>.</p>
<a name="introduction"></a>
<h2 id="introduction">Introduction</h2>
<p>Where Java and C++ are <i>statement-based</i> languages, the <a href="xmlprocessing.html">XQuery</a> language is <i>expression-based</i>. The simplest <a href="xmlprocessing.html">XQuery</a> expression is an XML element constructor:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>recipe<span class="operator">/</span><span class="operator">&gt;</span>

</pre>
<p>This <code>&lt;recipe/&gt;</code> element is an <a href="xmlprocessing.html">XQuery</a> expression that forms a complete <a href="xmlprocessing.html">XQuery</a>. In fact, this <a href="xmlprocessing.html">XQuery</a> doesn't actually query anything. It just creates an empty <code>&lt;recipe/&gt;</code> element in the output. But <a href="xquery-introduction.html#constructing-elements">constructing new elements in an XQuery</a> is often necessary.</p>
<p>An <a href="xmlprocessing.html">XQuery</a> expression can also be enclosed in curly braces and embedded in another <a href="xmlprocessing.html">XQuery</a> expression. This <a href="xmlprocessing.html">XQuery</a> has a document expression embedded in a node expression:</p>
<pre class="cpp">

   <span class="operator">&lt;</span>html xmlns<span class="operator">=</span><span class="string">&quot;http://www.w3.org/1999/xhtml/&quot;</span>
         xml:id<span class="operator">=</span><span class="string">&quot;{doc(&quot;</span>other<span class="operator">.</span>html<span class="string">&quot;)/html/@xml:id}&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>

</pre>
<p>It creates a new <code>&lt;html&gt;</code> element in the output and sets its <code>id</code> attribute to be the <code>id</code> attribute from an <code>&lt;html&gt;</code> element in the <code>other.html</code> file.</p>
<a name="using-path-expressions-to-match-and-select-items"></a>
<h2 id="using-path-expressions-to-match-and-select-items">Using Path Expressions To Match And Select Items</h2>
<p>In C++ and Java, we write nested <code>for</code> loops and recursive functions to traverse XML trees in search of elements of interest. In <a href="xmlprocessing.html">XQuery</a>, we write these iterative and recursive algorithms with <i>path expressions</i>.</p>
<p>A path expression looks somewhat like a typical <i>file pathname</i> for locating a file in a hierarchical file system. It is a sequence of one or more <i>steps</i> separated by slash '/' or double slash '//'. Although path expressions are used for traversing XML trees, not file systems, in Qt XML Patterns we can model a file system to look like an XML tree, so in Qt XML Patterns we can use <a href="xmlprocessing.html">XQuery</a> to traverse a file system. See the <a href="qtxmlpatterns-filetree-example.html">file system example</a>.</p>
<p>Think of a path expression as an algorithm for traversing an XML tree to find and collect items of interest. This algorithm is evaluated by evaluating each step moving from left to right through the sequence. A step is evaluated with a set of input items (nodes and atomic values), sometimes called the <i>focus</i>. The step is evaluated for each item in the focus. These evaluations produce a new set of items, called the <i>result</i>, which then becomes the focus that is passed to the next step. Evaluation of the final step produces the final result, which is the result of the <a href="xmlprocessing.html">XQuery</a>. The items in the result set are presented in <a href="http://www.w3.org/TR/xquery/#id-document-order">document order</a> and without duplicates.</p>
<p>With Qt XML Patterns, a standard way to present the initial focus to a query is to call <a href="qxmlquery.html#setFocus">QXmlQuery::setFocus</a>(). Another common way is to let the <a href="xmlprocessing.html">XQuery</a> itself create the initial focus by using the first step of the path expression to call the <a href="xmlprocessing.html">XQuery</a> <code>doc()</code> function. The <code>doc()</code> function loads an XML document and returns the <i>document node</i>. Note that the document node is <i>not</i> the same as the <i>document element</i>. The <i>document node</i> is a node constructed in memory, when the document is loaded. It represents the entire XML document, not the document element. The <i>document element</i> is the single, top-level XML element in the file. The <code>doc()</code> function returns the document node, which becomes the singleton node in the initial focus set. The document node will have one child node, and that child node will represent the document element. Consider the following <a href="xmlprocessing.html">XQuery</a>:</p>
<pre class="cpp">

  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//recipe</span>

</pre>
<p>The <code>doc()</code> function loads the <code>cookbook.xml</code> file and returns the document node. The document node then becomes the focus for the next step <code>//recipe</code>. Here the double slash means select all <code>&lt;recipe&gt;</code> elements found below the document node, regardless of where they appear in the document tree. The query selects all <code>&lt;recipe&gt;</code> elements in the cookbook. See <a href="xquery-introduction.html#running-the-cookbook-examples">Running The Cookbook Examples</a> for instructions on how to run this query (and most of the ones that follow) from the command line.</p>
<p>Conceptually, evaluation of the steps of a path expression is similar to iterating through the same number of nested <i>for</i> loops. Consider the following <a href="xmlprocessing.html">XQuery</a>, which builds on the previous one:</p>
<pre class="cpp">

  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//recipe/title</span>

</pre>
<p>This <a href="xmlprocessing.html">XQuery</a> is a single path expression composed of three steps. The first step creates the initial focus by calling the <code>doc()</code> function. We can paraphrase what the query engine does at each step:</p>
<ol class="1" type="1"><li>for each node in the initial focus (the document node)..&#x2e;</li>
<li>for each descendant node that is a <code>&lt;recipe&gt;</code> element..&#x2e;</li>
<li>collect the child nodes that are <code>&lt;title&gt;</code> elements.</li>
</ol>
<p>Again the double slash means select all the <code>&lt;recipe&gt;</code> elements in the document. The single slash before the <code>&lt;title&gt;</code> element means select only those <code>&lt;title&gt;</code> elements that are <i>child</i> elements of a <code>&lt;recipe&gt;</code> element (i.e&#x2e; not grandchildren, etc). The <a href="xmlprocessing.html">XQuery</a> evaluates to a final result set containing the <code>&lt;title&gt;</code> element of each <code>&lt;recipe&gt;</code> element in the cookbook.</p>
<a name="axis-steps"></a>
<h3 >Axis Steps</h3>
<p>The most common kind of path step is called an <i>axis step</i>, which tells the query engine which way to navigate from the context node, and which test to perform when it encounters nodes along the way. An axis step has two parts, an <i>axis specifier</i>, and a <i>node test</i>. Conceptually, evaluation of an axis step proceeds as follows: For each node in the focus set, the query engine navigates out from the node along the specified axis and applies the node test to each node it encounters. The nodes selected by the node test are collected in the result set, which becomes the focus set for the next step.</p>
<p>In the example <a href="xmlprocessing.html">XQuery</a> above, the second and third steps are both axis steps. Both apply the <code>element(name)</code> node test to nodes encountered while traversing along some axis. But in this example, the two axis steps are written in a <a href="xquery-introduction.html#shorthand-form">shorthand form</a>, where the axis specifier and the node test are not written explicitly but are implied. XQueries are normally written in this shorthand form, but they can also be written in the longhand form. If we rewrite the <a href="xmlprocessing.html">XQuery</a> in the longhand form, it looks like this:</p>
<pre class="cpp">

  doc(<span class="char">'cookbook.xml'</span>)<span class="operator">/</span>descendant<span class="operator">-</span><span class="keyword">or</span><span class="operator">-</span>self<span class="operator">::</span>element(recipe)<span class="operator">/</span>child<span class="operator">::</span>element(title)

</pre>
<p>The two axis steps have been expanded. The first step (<code>//recipe</code>) has been rewritten as <code>/descendant-or-self::element(recipe)</code>, where <code>descendant-or-self::</code> is the axis specifier and <code>element(recipe)</code> is the node test. The second step (<code>title</code>) has been rewritten as <code>/child::element(title)</code>, where <code>child::</code> is the axis specifier and <code>element(title)</code> is the node test. The output of the expanded <a href="xmlprocessing.html">XQuery</a> will be exactly the same as the output of the shorthand form.</p>
<p>To create an axis step, concatenate an axis specifier and a node test. The following sections list the axis specifiers and node tests that are available.</p>
<a name="axis-specifiers"></a>
<h3 >Axis Specifiers</h3>
<p>An axis specifier defines the direction you want the query engine to take, when it navigates away from the context node. Qt XML Patterns supports the following axes.</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Axis Specifier</th><th >refers to the axis containing..&#x2e;</th></tr></thead>
<tr valign="top" class="odd"><td ><code>self::</code></td><td >the context node itself</td></tr>
<tr valign="top" class="even"><td ><code>attribute::</code></td><td >all attribute nodes of the context node</td></tr>
<tr valign="top" class="odd"><td ><code>child::</code></td><td >all child nodes of the context node (not attributes)</td></tr>
<tr valign="top" class="even"><td ><code>descendant::</code></td><td >all descendants of the context node (children, grandchildren, etc)</td></tr>
<tr valign="top" class="odd"><td ><code>descendant-or-self::</code></td><td >all nodes in <code>descendant</code> + <code>self</code></td></tr>
<tr valign="top" class="even"><td ><code>parent::</code></td><td >the parent node of the context node, or empty if there is no parent</td></tr>
<tr valign="top" class="odd"><td ><code>ancestor::</code></td><td >all ancestors of the context node (parent, grandparent, etc)</td></tr>
<tr valign="top" class="even"><td ><code>ancestor-or-self::</code></td><td >all nodes in <code>ancestor</code> + <code>self</code></td></tr>
<tr valign="top" class="odd"><td ><code>following::</code></td><td >all nodes in the tree containing the context node, <i>not</i> including <code>descendant</code>, <i>and</i> that follow the context node in the document</td></tr>
<tr valign="top" class="even"><td ><code>preceding::</code></td><td >all nodes in the tree contianing the context node, <i>not</i> including <code>ancestor</code>, <i>and</i> that precede the context node in the document</td></tr>
<tr valign="top" class="odd"><td ><code>following-sibling::</code></td><td >all children of the context node's <code>parent</code> that follow the context node in the document</td></tr>
<tr valign="top" class="even"><td ><code>preceding-sibling::</code></td><td >all children of the context node's <code>parent</code> that precede the context node in the document</td></tr>
</table></div>
<a name="node-tests"></a>
<h3 >Node Tests</h3>
<p>A node test is a conditional expression that must be true for a node if the node is to be selected by the axis step. The conditional expression can test just the <i>kind</i> of node, or it can test the <i>kind</i> of node and the <i>name</i> of the node. The <a href="xmlprocessing.html">XQuery</a> specification for <a href="http://www.w3.org/TR/xquery/#node-tests">node tests</a> also defines a third condition, the node's <i>Schema Type</i>, but schema type tests are not supported in Qt XML Patterns.</p>
<p>Qt XML Patterns supports the following node tests. The tests that have a <code>name</code> parameter test the node's name in addition to its <i>kind</i> and are often called the <a href="xquery-introduction.html#name-tests">Name Tests</a>.</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Node Test</th><th >matches all..&#x2e;</th></tr></thead>
<tr valign="top" class="odd"><td ><code>node()</code></td><td >nodes of any kind</td></tr>
<tr valign="top" class="even"><td ><code>text()</code></td><td >text nodes</td></tr>
<tr valign="top" class="odd"><td ><code>comment()</code></td><td >comment nodes</td></tr>
<tr valign="top" class="even"><td ><code>element()</code></td><td >element nodes (same as star: *)</td></tr>
<tr valign="top" class="odd"><td ><code>element(name)</code></td><td >element nodes named <code>name</code></td></tr>
<tr valign="top" class="even"><td ><code>attribute()</code></td><td >attribute nodes</td></tr>
<tr valign="top" class="odd"><td ><code>attribute(name)</code></td><td >attribute nodes named <code>name</code></td></tr>
<tr valign="top" class="even"><td ><code>processing-instruction()</code></td><td >processing-instructions</td></tr>
<tr valign="top" class="odd"><td ><code>processing-instruction(name)</code></td><td >processing-instructions named <code>name</code></td></tr>
<tr valign="top" class="even"><td ><code>document-node()</code></td><td >document nodes (there is only one)</td></tr>
<tr valign="top" class="odd"><td ><code>document-node(element(name))</code></td><td >document node with document element <code>name</code></td></tr>
</table></div>
<a name="shorthand-form"></a><a name="shorthand-form"></a>
<h3 >Shorthand Form</h3>
<p>Writing axis steps using the longhand form with axis specifiers and node tests is semantically clear but syntactically verbose. The shorthand form is easy to learn and, once you learn it, just as easy to read. In the shorthand form, the axis specifier and node test are implied by the syntax. XQueries are normally written in the shorthand form. Here is a table of some frequently used shorthand forms:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Shorthand syntax</th><th >Short for..&#x2e;</th><th >matches all..&#x2e;</th></tr></thead>
<tr valign="top" class="odd"><td ><code>name</code></td><td ><code>child::element(name)</code></td><td >child nodes that are <code>name</code> elements</td></tr>
<tr valign="top" class="even"><td ><code>*</code></td><td ><code>child::element()</code></td><td >child nodes that are elements (<code>node()</code> matches <i>all</i> child nodes)</td></tr>
<tr valign="top" class="odd"><td ><code>..</code></td><td ><code>parent::node()</code></td><td >parent nodes (there is only one)</td></tr>
<tr valign="top" class="even"><td ><code>@*</code></td><td ><code>attribute::attribute()</code></td><td >attribute nodes</td></tr>
<tr valign="top" class="odd"><td ><code>@name</code></td><td ><code>attribute::attribute(name)</code></td><td ><code>name</code> attributes</td></tr>
<tr valign="top" class="even"><td ><code>//</code></td><td ><code>descendant-or-self::node()</code></td><td >descendent nodes (when used instead of '/')</td></tr>
</table></div>
<p>The <a href="http://www.w3.org/TR/xquery/">XQuery language specification</a> has a more detailed section on the shorthand form, which it calls the <a href="http://www.w3.org/TR/xquery/#abbrev">abbreviated syntax</a>. More examples of path expressions written in the shorthand form are found there. There is also a section listing examples of path expressions written in the <a href="http://www.w3.org/TR/xquery/#unabbrev">longhand form</a>.</p>
<a name="name-tests"></a><a name="name-tests"></a>
<h3 >Name Tests</h3>
<p>The name tests are the <a href="xquery-introduction.html#node-tests">Node Tests</a> that have the <code>name</code> parameter. A name test must match the node <i>name</i> in addition to the node <i>kind</i>. We have already seen name tests used:</p>
<pre class="cpp">

  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//recipe/title</span>

</pre>
<p>In this path expression, both <code>recipe</code> and <code>title</code> are name tests written in the shorthand form. <a href="xmlprocessing.html">XQuery</a> resolves these names (<a href="http://www.w3.org/TR/xquery/#id-basics">QNames</a>) to their expanded form using whatever <a href="http://www.w3.org/TR/xquery/#dt-namespace-declaration">namespace declarations</a> it knows about. Resolving a name to its expanded form means replacing its namespace prefix, if one is present (there aren't any present in the example), with a namespace URI. The expanded name then consists of the namespace URI and the local name.</p>
<p>But the names in the example above don't have namespace prefixes, because we didn't include a namespace declaration in our <code>cookbook.xml</code> file. However, we will often use <a href="xmlprocessing.html">XQuery</a> to query XML documents that use namespaces. Forgetting to declare the correct namespace(s) in an <a href="xmlprocessing.html">XQuery</a> is a common cause of <a href="xmlprocessing.html">XQuery</a> failures. Let's add a <i>default</i> namespace to <code>cookbook.xml</code> now. Change the <i>document element</i> in <code>cookbook.xml</code> from:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>cookbook<span class="operator">&gt;</span>

</pre>
<p>to..&#x2e;</p>
<pre class="cpp">

  <span class="operator">&lt;</span>cookbook xmlns<span class="operator">=</span><span class="string">&quot;http://cookbook/namespace&quot;</span><span class="operator">&gt;</span>

</pre>
<p>This is called a <i>default namespace</i> declaration because it doesn't include a namespace prefix. By including this default namespace declaration in the document element, we mean that all unprefixed <i>element</i> names in the document, including the document element itself (<code>cookbook</code>), are automatically in the default namespace <code>http://cookbook/namespace</code>. Note that unprefixed <i>attribute</i> names are not affected by the default namespace declaration. They are always considered to be in <i>no namespace</i>. Note also that the URL we choose as our namespace URI need not refer to an actual location, and doesn't refer to one in this case. But click on <a href="http://www.w3.org/XML/1998/namespace">http://www.w3.org/XML/1998/namespace</a>, for example, which is the namespace URI for elements and attributes prefixed with <code>xml:</code>.</p>
<p>Now when we try to run the previous <a href="xmlprocessing.html">XQuery</a> example, no output is produced! The path expression no longer matches anything in the cookbook file because our <a href="xmlprocessing.html">XQuery</a> doesn't yet know about the namespace declaration we added to the cookbook document. There are two ways we can declare the namespace in the <a href="xmlprocessing.html">XQuery</a>. We can give it a <i>namespace prefix</i> (e.g&#x2e; <code>c</code> for cookbook) and prefix each name test with the namespace prefix:</p>
<pre class="cpp">

  declare <span class="keyword">namespace</span> c <span class="operator">=</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//c:recipe/c:title</span>

</pre>
<p>Or we can declare the namespace to be the <i>default element namespace</i>, and then we can still run the original <a href="xmlprocessing.html">XQuery</a>:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//recipe/title</span>

</pre>
<p>Both methods will work and produce the same output, all the <code>&lt;title&gt;</code> elements:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>title xmlns<span class="operator">=</span><span class="string">&quot;http://cookbook/namespace&quot;</span><span class="operator">&gt;</span>Quick and Easy Mushroom Soup<span class="operator">&lt;</span><span class="operator">/</span>title<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>title xmlns<span class="operator">=</span><span class="string">&quot;http://cookbook/namespace&quot;</span><span class="operator">&gt;</span>Cheese on Toast<span class="operator">&lt;</span><span class="operator">/</span>title<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>title xmlns<span class="operator">=</span><span class="string">&quot;http://cookbook/namespace&quot;</span><span class="operator">&gt;</span>Hard<span class="operator">-</span>Boiled Eggs<span class="operator">&lt;</span><span class="operator">/</span>title<span class="operator">&gt;</span>

</pre>
<p>But note how the output is slightly different from the output we saw before we added the default namespace declaration to the cookbook file. Qt XML Patterns automatically includes the correct namespace attribute in each <code>&lt;title&gt;</code> element in the output. When Qt XML Patterns loads a document and expands a QName, it creates an instance of <a href="qxmlname.html">QXmlName</a>, which retains the namespace prefix along with the namespace URI and the local name. See <a href="qxmlname.html">QXmlName</a> for further details.</p>
<p>One thing to keep in mind from this namespace discussion, whether you run XQueries in a Qt program using Qt XML Patterns, or you run them from the command line using xmlpatterns, is that if you don't get the output you expect, it might be because the data you are querying uses namespaces, but you didn't declare those namespaces in your <a href="xmlprocessing.html">XQuery</a>.</p>
<a name="wildcards-in-name-tests"></a>
<h4 >Wildcards in Name Tests</h4>
<p>The wildcard <code>'*'</code> can be used in a name test. To find all the attributes in the cookbook but select only the ones in the <code>xml</code> namespace, use the <code>xml:</code> namespace prefix but replace the <i>local name</i> (the attribute name) with the wildcard:</p>
<pre class="cpp">

  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//@xml:*</span>

</pre>
<p>Oops! If you save this <a href="xmlprocessing.html">XQuery</a> in <code>file.xq</code> and run it through <code>xmlpatterns</code>, it doesn't work. You get an error message instead, something like this: <i>Error SENR0001 in file:///..&#x2e;file.xq, at line 1, column 1: Attribute xml:id can't be serialized because it appears at the top level.</i> The <a href="xmlprocessing.html">XQuery</a> actually ran correctly. It selected a bunch of <code>xml:id</code> attributes and put them in the result set. But then <code>xmlpatterns</code> sent the result set to a <a href="qxmlserializer.html">serializer</a>, which tried to output it as well-formed XML. Since the result set contains only attributes and attributes alone are not well-formed XML, the <a href="qxmlserializer.html">serializer</a> reports a <a href="http://www.w3.org/TR/2005/WD-xslt-xquery-serialization-20050915/#id-errors">serialization error</a>.</p>
<p>Fear not. <a href="xmlprocessing.html">XQuery</a> can do more than just find and select elements and attributes. It can <a href="xquery-introduction.html#constructing-elements">construct new ones on the fly</a> as well, which is what we need to do here if we want <code>xmlpatterns</code> to let us see the attributes we selected. The example above and the ones below are revisited in the <a href="xquery-introduction.html#constructing-elements">Constructing Elements</a> section. You can jump ahead to see the modified examples now, and then come back, or you can press on from here.</p>
<p>To find all the <code>name</code> attributes in the cookbook and select them all regardless of their namespace, replace the namespace prefix with the wildcard and write <code>name</code> (the attribute name) as the local name:</p>
<pre class="cpp">

  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//@*:name</span>

</pre>
<p>To find and select all the attributes of the <i>document element</i> in the cookbook, replace the entire name test with the wildcard:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>@<span class="operator">*</span>

</pre>
<a name="using-predicates-in-path-expressions"></a>
<h2 id="using-predicates-in-path-expressions">Using Predicates In Path Expressions</h2>
<p>Predicates can be used to further filter the nodes selected by a path expression. A predicate is an expression in square brackets ('[' and ']') that either returns a boolean value or a number. A predicate can appear at the end of any path step in a path expression. The predicate is applied to each node in the focus set. If a node passes the filter, the node is included in the result set. The query below selects the recipe element that has the <code>&lt;title&gt;</code> element <code>&quot;Hard-Boiled Eggs&quot;</code>.</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="string">&quot;cookbook.xml&quot;</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>recipe<span class="operator">[</span>title <span class="operator">=</span> <span class="string">&quot;Hard-Boiled Eggs&quot;</span><span class="operator">]</span>

</pre>
<p>The dot expression ('.') can be used in predicates and path expressions to refer to the current context node. The following query uses the dot expression to refer to the current <code>&lt;method&gt;</code> element. The query selects the empty <code>&lt;method&gt;</code> elements from the cookbook.</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//method[string-length(.) = 0]</span>

</pre>
<p>Note that passing the dot expression to the <a href="http://www.w3.org/TR/xpath-functions/#func-string-length">string-length()</a> function is optional. When <a href="http://www.w3.org/TR/xpath-functions/#func-string-length">string-length()</a> is called with no parameter, the context node is assumed:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//method[string-length() = 0]</span>

</pre>
<p>Actually, selecting an empty <code>&lt;method&gt;</code> element might not be very useful by itself. It doesn't tell you which recipe has the empty method:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>method xmlns<span class="operator">=</span><span class="string">&quot;http://cookbook/namespace&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>

</pre>
<a name="empty-method-not-robust"></a><p>What you probably want to see instead are the <code>&lt;recipe&gt;</code> elements that have empty <code>&lt;method&gt;</code> elements:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="comment">//recipe[string-length(method) = 0]</span>

</pre>
<p>The predicate uses the <a href="http://www.w3.org/TR/xpath-functions/#func-string-length">string-length()</a> function to test the length of each <code>&lt;method&gt;</code> element in each <code>&lt;recipe&gt;</code> element found by the node test. If a <code>&lt;method&gt;</code> contains no text, the predicate evaluates to <code>true</code> and the <code>&lt;recipe&gt;</code> element is selected. If the method contains some text, the predicate evaluates to <code>false</code>, and the <code>&lt;recipe&gt;</code> element is discarded. The output is the entire recipe that has no instructions for preparation:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>recipe xmlns<span class="operator">=</span><span class="string">&quot;http://cookbook/namespace&quot;</span> xml:id<span class="operator">=</span><span class="string">&quot;HardBoiledEggs&quot;</span><span class="operator">&gt;</span>
      <span class="operator">&lt;</span>title<span class="operator">&gt;</span>Hard<span class="operator">-</span>Boiled Eggs<span class="operator">&lt;</span><span class="operator">/</span>title<span class="operator">&gt;</span>
      <span class="operator">&lt;</span>ingredient name<span class="operator">=</span><span class="string">&quot;Eggs&quot;</span> quantity<span class="operator">=</span><span class="string">&quot;3&quot;</span> unit<span class="operator">=</span><span class="string">&quot;eggs&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
      <span class="operator">&lt;</span>time quantity<span class="operator">=</span><span class="string">&quot;3&quot;</span> unit<span class="operator">=</span><span class="string">&quot;minutes&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
      <span class="operator">&lt;</span>method<span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span><span class="operator">/</span>recipe<span class="operator">&gt;</span>

</pre>
<p>The astute reader will have noticed that this use of <code>string-length()</code> to find an empty element is unreliable. It works in this case, because the method element is written as <code>&lt;method/&gt;</code>, guaranteeing that its string length will be 0. It will still work if the method element is written as <code>&lt;method&gt;&lt;/method&gt;</code>, but it will fail if there is any whitespace between the opening and ending <code>&lt;method&gt;</code> tags. A more robust way to find the recipes with empty methods is presented in the section on <a href="xquery-introduction.html#boolean-predicates">Boolean Predicates</a>.</p>
<p>There are many more functions and operators defined for <a href="xmlprocessing.html">XQuery</a> and XPath. They are all <a href="http://www.w3.org/TR/xpath-functions">documented in the specification</a>.</p>
<a name="positional-predicates"></a>
<h3 >Positional Predicates</h3>
<p>Predicates are often used to filter items based on their position in a sequence. For path expressions processing items loaded from XML documents, the normal sequence is <a href="http://www.w3.org/TR/xquery/#id-document-order">document order</a>. This query returns the second <code>&lt;recipe&gt;</code> element in the <code>cookbook.xml</code> file:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>recipe<span class="operator">[</span><span class="number">2</span><span class="operator">]</span>

</pre>
<p>The other frequently used positional function is <a href="http://www.w3.org/TR/xpath-functions/#func-last">last()</a>, which returns the numeric position of the last item in the focus set. Stated another way, <a href="http://www.w3.org/TR/xpath-functions/#func-last">last()</a> returns the size of the focus set. This query returns the last recipe in the cookbook:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>recipe<span class="operator">[</span>last()<span class="operator">]</span>

</pre>
<p>And this query returns the next to last <code>&lt;recipe&gt;</code>:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>recipe<span class="operator">[</span>last() <span class="operator">-</span> <span class="number">1</span><span class="operator">]</span>

</pre>
<a name="boolean-predicates"></a>
<h3 >Boolean Predicates</h3>
<p>The other kind of predicate evaluates to <i>true</i> or <i>false</i>. A boolean predicate takes the value of its expression and determines its <i>effective boolean value</i> according to the following rules:</p>
<ul>
<li>An expression that evaluates to a single node is <code>true</code>.</li>
<li>An expression that evaluates to a string is <code>false</code> if the string is empty and <code>true</code> if the string is not empty.</li>
<li>An expression that evaluates to a boolean value (i.e&#x2e; type <code>xs:boolean</code>) is that value.</li>
<li>If the expression evaluates to anything else, it's an error (e.g&#x2e; type <code>xs:date</code>).</li>
</ul>
<p>We have already seen some boolean predicates in use. Earlier, we saw a <i>not so robust</i> way to find the <a href="xquery-introduction.html#empty-method-not-robust">recipes that have no instructions</a>. <code>[string-length(method) = 0]</code> is a boolean predicate that would fail in the example if the empty method element was written with both opening and closing tags and there was whitespace between the tags. Here is a more robust way that uses a different boolean predicate.</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>recipe<span class="operator">[</span>method<span class="operator">[</span>empty(step)<span class="operator">]</span><span class="operator">]</span>

</pre>
<p>This one uses the <a href="http://www.w3.org/TR/xpath-functions/#func-empty">empty()</a> and function to test whether the method contains any steps. If the method contains no steps, then <code>empty(step)</code> will return <code>true</code>, and hence the predicate will evaluate to <code>true</code>.</p>
<p>But even that version isn't foolproof. Suppose the method does contain steps, but all the steps themselves are empty. That's still a case of a recipe with no instructions that won't be detected. There is a better way:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>recipe<span class="operator">[</span><span class="keyword">not</span>(normalize<span class="operator">-</span>space(method))<span class="operator">]</span>

</pre>
<p>This version uses the <a href="http://www.w3.org/TR/xpath-functions/#func-not">not</a> and <a href="http://www.w3.org/TR/xpath-functions/#func-normalize-space">normalize-space()</a> functions. <code>normalize-space(method))</code> returns the contents of the method element as a string, but with all the whitespace normalized, i.e&#x2e;, the string value of each <code>&lt;step&gt;</code> element will have its whitespace normalized, and then all the normalized step values will be concatenated. If that string is empty, then <code>not()</code> returns <code>true</code> and the predicate is <code>true</code>.</p>
<p>We can also use the <a href="http://www.w3.org/TR/xpath-functions/#func-position">position()</a> function in a comparison to inspect positions with conditional logic. The <a href="http://www.w3.org/TR/xpath-functions/#func-position">position()</a> function returns the position index of the current context item in the sequence of items:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>recipe<span class="operator">[</span>position() <span class="operator">=</span> <span class="number">2</span><span class="operator">]</span>

</pre>
<p>Note that the first position in the sequence is position 1, not 0. We can also select <i>all</i> the recipes after the first one:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  doc(<span class="char">'cookbook.xml'</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>recipe<span class="operator">[</span>position() <span class="operator">&gt;</span> <span class="number">1</span><span class="operator">]</span>

</pre>
<a name="constructing-elements"></a><a name="constructing-elements"></a>
<h2 id="constructing-elements">Constructing Elements</h2>
<p>In the section about <a href="xquery-introduction.html#wildcards-in-name-tests">using wildcards in name tests</a>, we saw three simple example XQueries, each of which selected a different list of XML attributes from the cookbook. We couldn't use <code>xmlpatterns</code> to run these queries, however, because <code>xmlpatterns</code> sends the <a href="xmlprocessing.html">XQuery</a> results to a <a href="qxmlserializer.html">serializer</a>, which expects to serialize the results as well-formed XML. Since a list of XML attributes by itself is not well-formed XML, the serializer reported an error for each <a href="xmlprocessing.html">XQuery</a>.</p>
<p>Since an attribute must appear in an element, for each attribute in the result set, we must create an XML element. We can do that using a <a href="http://www.w3.org/TR/xquery/#id-for-let"><i>for</i> clause</a> with a <a href="http://www.w3.org/TR/xquery/#id-variables">bound variable</a>, and a <a href="http://www.w3.org/TR/xquery/#id-orderby-return"><i>return</i> clause</a> with an element constructor:</p>
<pre class="cpp">

  <span class="keyword">for</span> $i in doc(<span class="string">&quot;cookbook.xml&quot;</span>)<span class="comment">//@xml:*</span>
  <span class="keyword">return</span> <span class="operator">&lt;</span>p<span class="operator">&gt;</span>{$i}<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>

</pre>
<p>The <i>for</i> clause produces a sequence of attribute nodes from the result of the path expression. Each attribute node in the sequence is bound to the variable <code>$i</code>. The <i>return</i> clause then constructs a <code>&lt;p&gt;</code> element around the attribute node. Here is the output:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>p xml:id<span class="operator">=</span><span class="string">&quot;MushroomSoup&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p xml:id<span class="operator">=</span><span class="string">&quot;CheeseOnToast&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p xml:id<span class="operator">=</span><span class="string">&quot;HardBoiledEggs&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>

</pre>
<p>The output contains one <code>&lt;p&gt;</code> element for each <code>xml:id</code> attribute in the cookbook. Note that <a href="xmlprocessing.html">XQuery</a> puts each attribute in the right place in its <code>&lt;p&gt;</code> element, despite the fact that in the <i>return</i> clause, the <code>$i</code> variable is positioned as if it is meant to become <code>&lt;p&gt;</code> element content.</p>
<p>The other two examples from the <a href="xquery-introduction.html#wildcards-in-name-tests">wildcard</a> section can be rewritten the same way. Here is the <a href="xmlprocessing.html">XQuery</a> that selects all the <code>name</code> attributes, regardless of namespace:</p>
<pre class="cpp">

  <span class="keyword">for</span> $i in doc(<span class="string">&quot;cookbook.xml&quot;</span>)<span class="comment">//@*:name</span>
  <span class="keyword">return</span> <span class="operator">&lt;</span>p<span class="operator">&gt;</span>{$i}<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>

</pre>
<p>And here is its output:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Fresh mushrooms&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Garlic&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Olive oil&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Milk&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Water&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Cream&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Vegetable soup cube&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Ground black pepper&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Dried parsley&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Bread&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Cheese&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p name<span class="operator">=</span><span class="string">&quot;Eggs&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>

</pre>
<p>And here is the <a href="xmlprocessing.html">XQuery</a> that selects all the attributes from the <i>document element</i>:</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  <span class="keyword">for</span> $i in doc(<span class="string">&quot;cookbook.xml&quot;</span>)<span class="operator">/</span>cookbook<span class="operator">/</span>@<span class="operator">*</span>
  <span class="keyword">return</span> <span class="operator">&lt;</span>p<span class="operator">&gt;</span>{$i}<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>

</pre>
<p>And here is its output:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>p xmlns<span class="operator">=</span><span class="string">&quot;http://cookbook/namespace&quot;</span> count<span class="operator">=</span><span class="string">&quot;3&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>

</pre>
<a name="element-constructors-are-expressions"></a>
<h3 >Element Constructors are Expressions</h3>
<p>Because node constructors are expressions, they can be used in XQueries wherever expressions are allowed.</p>
<pre class="cpp">

  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  let $docURI :<span class="operator">=</span> <span class="char">'cookbook.xml'</span>
  <span class="keyword">return</span> <span class="keyword">if</span>(doc<span class="operator">-</span>available($docURI))
         then doc($docURI)<span class="comment">//recipe/&lt;oppskrift&gt;{./node()}&lt;/oppskrift&gt;</span>
         <span class="keyword">else</span> <span class="operator">&lt;</span>oppskrift<span class="operator">&gt;</span>Failed to load {$docURI}<span class="operator">&lt;</span><span class="operator">/</span>oppskrift<span class="operator">&gt;</span>

</pre>
<p>If <code>cookbook.xml</code> is loaded without error, a <code>&lt;oppskrift&gt;</code> element (Norwegian word for recipe) is constructed for each <code>&lt;recipe&gt;</code> element in the cookbook, and the child nodes of the <code>&lt;recipe&gt;</code> are copied into the <code>&lt;oppskrift&gt;</code> element. But if the cookbook document doesn't exist or does not contain well-formed XML, a single <code>&lt;oppskrift&gt;</code> element is constructed containing an error message.</p>
<a name="constructing-atomic-values"></a>
<h2 id="constructing-atomic-values">Constructing Atomic Values</h2>
<p><a href="xmlprocessing.html">XQuery</a> also has atomic values. An atomic value is a value in the value space of one of the built-in datatypes in the <a href="http://www.w3.org/TR/xmlschema-2">XML Schema language</a>. These <i>atomic types</i> have built-in operators for doing arithmetic, comparisons, and for converting values to other atomic types. See the <a href="http://www.w3.org/TR/xmlschema-2/#built-in-datatypes">Built-in Datatype Hierarchy</a> for the entire tree of built-in, primitive and derived atomic types.</p>
<p><b>Note: </b>Click on a data type in the tree for its detailed specification.</p><p>To construct an atomic value as element content, enclose an expression in curly braces and embed it in the element constructor:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>e<span class="operator">&gt;</span>{sum((<span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">3</span>))}<span class="operator">&lt;</span><span class="operator">/</span>e<span class="operator">&gt;</span>

</pre>
<p>Sending this <a href="xmlprocessing.html">XQuery</a> through xmlpatterns produces:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>e<span class="operator">&gt;</span><span class="number">6</span><span class="operator">&lt;</span><span class="operator">/</span>e<span class="operator">&gt;</span>

</pre>
<p>To compute the value of an attribute, enclose the expression in curly braces and embed it in the attribute value:</p>
<pre class="cpp">

  declare variable $insertion :<span class="operator">=</span> <span class="string">&quot;example&quot;</span>;
  <span class="operator">&lt;</span>p <span class="keyword">class</span><span class="operator">=</span><span class="string">&quot;important {$insertion} obsolete&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>

</pre>
<p>Sending this <a href="xmlprocessing.html">XQuery</a> through xmlpatterns produces:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>p <span class="keyword">class</span><span class="operator">=</span><span class="string">&quot;important example obsolete&quot;</span><span class="operator">/</span><span class="operator">&gt;</span>
  declare <span class="keyword">default</span> element <span class="keyword">namespace</span> <span class="string">&quot;http://cookbook/namespace&quot;</span>;
  let $docURI :<span class="operator">=</span> <span class="char">'cookbook.xml'</span>
  <span class="keyword">return</span> <span class="keyword">if</span>(doc<span class="operator">-</span>available($docURI))
         then doc($docURI)<span class="comment">//recipe/&lt;oppskrift&gt;{./node()}&lt;/oppskrift&gt;</span>
         <span class="keyword">else</span> <span class="operator">&lt;</span>oppskrift<span class="operator">&gt;</span>Failed to load {$docURI}<span class="operator">&lt;</span><span class="operator">/</span>oppskrift<span class="operator">&gt;</span>

</pre>
<p>If <code>cookbook.xml</code> is loaded without error, a <code>&lt;oppskrift&gt;</code> element (Norweigian word for recipe) is constructed for each <code>&lt;recipe&gt;</code> element in the cookbook, and the child nodes of the <code>&lt;recipe&gt;</code> are copied into the <code>&lt;oppskrift&gt;</code> element. But if the cookbook document doesn't exist or does not contain well-formed XML, a single <code>&lt;oppskrift&gt;</code> element is constructed containing an error message.</p>
<a name="running-the-cookbook-examples"></a>
<h2 id="running-the-cookbook-examples">Running The Cookbook Examples</h2>
<p>Most of the <a href="xmlprocessing.html">XQuery</a> examples in this document refer to the <code>cookbook.xml</code> example file from the <a href="qtxmlpatterns-recipes-example.html">Recipes Example</a>. Copy the <code>cookbook.xml</code> to your current directory, save one of the cookbook <a href="xmlprocessing.html">XQuery</a> examples in a <code>.xq</code> file (e.g&#x2e;, <code>file.xq</code>), and run the <a href="xmlprocessing.html">XQuery</a> using Qt's command line utility:</p>
<pre class="cpp">

  xmlpatterns file<span class="operator">.</span>xq

</pre>
<a name="further-reading"></a>
<h2 id="further-reading">Further Reading</h2>
<p>There is much more to the <a href="xmlprocessing.html">XQuery</a> language than we have presented in this short introduction. We will be adding more here in later releases. In the meantime, playing with the <code>xmlpatterns</code> utility and making modifications to the <a href="xmlprocessing.html">XQuery</a> examples provided here will be quite informative. An <a href="xmlprocessing.html">XQuery</a> textbook will be a good investment.</p>
<p>You can also ask questions on <a href="xmlprocessing.html">XQuery</a> mail lists:</p>
<ul>
<li><a href="http://lists.qt-project.org/mailman/listinfo/interest">qt-interest</a></li>
<li><a href="http://www.x-query.com/mailman/listinfo/talk">talk at x-query.com</a>.</li>
</ul>
<p><a href="http://www.functx.com/functx/">FunctX</a> has a collection of <a href="xmlprocessing.html">XQuery</a> functions that can be both useful and educational.</p>
<p>This introduction contains many links to the specifications, which, of course, are the ultimate source of information about <a href="xmlprocessing.html">XQuery</a>. They can be a bit difficult, though, so consider investing in a textbook:</p>
<ul>
<li><a href="http://www.w3.org/TR/xquery/">XQuery 1.0: An XML Query Language</a> - the main source for syntax and semantics.</li>
<li><a href="http://www.w3.org/TR/xpath-functions/">XQuery 1.0 and XPath 2.0 Functions and Operators</a> - the builtin functions and operators.</li>
</ul>
<a name="faq"></a>
<h2 id="faq">FAQ</h2>
<p>The answers to these frequently asked questions explain the causes of several common mistakes that most beginners make. Reading through the answers ahead of time might save you a lot of head scratching.</p>
<a name="why-didn-t-my-path-expression-match-anything"></a>
<h3 >Why didn't my path expression match anything?</h3>
<p>The most common cause of this bug is failure to declare one or more namespaces in your <a href="xmlprocessing.html">XQuery</a>. Consider the following query for selecting all the examples in an XHTML document:</p>
<pre class="cpp">

  doc(&quot;index.html&quot;)/html/body/p[@class=&quot;example&quot;]

</pre>
<p>It won't match anything because <code>index.html</code> is an XHTML file, and all XHTML files declare the default namespace <code>&quot;http://www.w3.org/1999/xhtml&quot;</code> in their top (<code>&lt;html&gt;</code>) element. But the query doesn't declare this namespace, so the path expression expands <code>html</code> to <code>{}html</code> and tries to match that expanded name. But the actual expanded name is <code>{http://www.w3.org/1999/xhtml}html</code>. One possible fix is to declare the correct default namespace in the <a href="xmlprocessing.html">XQuery</a>:</p>
<pre class="cpp">

  declare namespace x = &quot;http://www.w3.org/1999/xhtml/&quot;;
  doc(&quot;index.html&quot;)/x:html/x:body/x:p[@class=&quot;example&quot;]

</pre>
<p>Another common cause of this bug is to confuse the <i>document node</i> with the top element node. They are different. This query won't match anything:</p>
<pre class="cpp">

  doc(&quot;myPlainHTML.html&quot;)/body

</pre>
<p>The <code>doc()</code> function returns the <i>document node</i>, not the top element node (<code>&lt;html&gt;</code>). Don't forget to match the top element node in the path expression:</p>
<pre class="cpp">

  doc(&quot;myPlainHTML.html&quot;)/html/body

</pre>
<a name="what-if-my-input-namespace-is-different-from-my-output-namespace"></a>
<h3 >What if my input namespace is different from my output namespace?</h3>
<p>Just remember to declare both namespaces in your <a href="xmlprocessing.html">XQuery</a> and use them properly. Consider the following query, which is meant to generate XHTML output from XML input:</p>
<pre class="cpp">

  declare default element namespace &quot;http://www.w3.org/1999/xhtml&quot;;
  &lt;html&gt;
      &lt;body&gt;
          {
              for $i in doc(&quot;testResult.xml&quot;)/tests/test[@status = &quot;failure&quot;]
              order by $i/@name
              return &lt;p&gt;{$i/@name}&lt;/p&gt;
          }
      &lt;/body&gt;
  &lt;/html&gt;

</pre>
<p>We want the <code>&lt;html&gt;</code>, <code>&lt;body&gt;</code>, and <code>&lt;p&gt;</code> nodes we create in the output to be in the standard XHTML namespace, so we declare the default namespace to be <code>http://www.w3.org/1999/xhtml</code>. That's correct for the output, but that same default namespace will also be applied to the node names in the path expression we're trying to match in the input (<code>/tests/test[@status = &quot;failure&quot;]</code>), which is wrong, because the namespace used in <code>testResult.xml</code> is perhaps in the empty namespace. So we must declare that namespace too, with a namespace prefix, and then use the prefix with the node names in the path expression. This one will probably work better:</p>
<pre class="cpp">

  declare namespace x = &quot;http://www.w3.org/1998/xhtml&quot;;
  &lt;x:html&gt;
      &lt;x:body&gt;
          {
              for $i in doc(&quot;testResult.xml&quot;)/tests/test[@status = &quot;failure&quot;]
              order by $i/@name
              return &lt;x:p&gt;{$i/@name}&lt;/x:p&gt;
          }
      &lt;/x:body&gt;
  &lt;/x:html&gt;

</pre>
<a name="why-doesn-t-my-return-clause-work"></a>
<h3 >Why doesn't my return clause work?</h3>
<p>Recall that <a href="xmlprocessing.html">XQuery</a> is an <i>expression-based</i> language, not <i>statement-based</i>. Because an <a href="xmlprocessing.html">XQuery</a> is a lot of expressions, understanding <a href="xmlprocessing.html">XQuery</a> expression precedence is very important. Consider the following query:</p>
<pre class="cpp">

  for $i in(reverse(1 to 10)),
      $d in xs:integer(doc(&quot;numbers.xml&quot;)/numbers/number)
  return $i + $d

</pre>
<p>It looks ok, but it isn't. It is supposed to be a FLWOR expression comprising a <i>for</i> clause and a <i>return</i> clause, but it isn't just that. It <i>has</i> a FLWOR expression, certainly (with the <i>for</i> and <i>return</i> clauses), but it <i>also</i> has an arithmetic expression (<i>+ $d</i>) dangling at the end because we didn't enclose the return expression in parentheses.</p>
<p>Using parentheses to establish precedence is more important in <a href="xmlprocessing.html">XQuery</a> than in other languages, because <a href="xmlprocessing.html">XQuery</a> is <i>expression-based</i>. In In this case, without parantheses enclosing <code>$i + $d</code>, the return clause only returns <code>$i</code>. The <code>+$d</code> will have the result of the FLWOR expression as its left operand. And, since the scope of variable <code>$d</code> ends at the end of the <i>return</i> clause, a variable out of scope error will be reported. Correct these problems by using parentheses.</p>
<pre class="cpp">

  for $i in(reverse(1 to 10)),
      $d in xs:integer(doc(&quot;numbers.xml&quot;)/numbers/number)
  return ($i + $d)

</pre>
<a name="why-didn-t-my-expression-get-evaluated"></a>
<h3 >Why didn't my expression get evaluated?</h3>
<p>You probably misplaced some curly braces. When you want an expression evaluated inside an element constructor, enclose the expression in curly braces. Without the curly braces, the expression will be interpreted as text. Here is a <code>sum()</code> expression used in an <code>&lt;e&gt;</code> element. The table shows cases where the curly braces are missing, misplaced, and placed correctly:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >element constructor with expression..&#x2e;</th><th >evaluates to..&#x2e;</th></tr></thead>
<tr valign="top" class="odd"><td >&lt;e&gt;sum((1, 2, 3))&lt;/e&gt;</td><td >&lt;e&gt;sum((1, 2, 3))&lt;/e&gt;</td></tr>
<tr valign="top" class="even"><td >&lt;e&gt;sum({(1, 2, 3)})&lt;/e&gt;</td><td >&lt;e&gt;sum(1 2 3)&lt;/e&gt;</td></tr>
<tr valign="top" class="odd"><td >&lt;e&gt;{sum((1, 2, 3))}&lt;/e&gt;</td><td >&lt;e&gt;6&lt;/e&gt;</td></tr>
</table></div>
<a name="my-predicate-is-correct-so-why-doesn-t-it-select-the-right-stuff"></a>
<h3 >My predicate is correct, so why doesn't it select the right stuff?</h3>
<p>Either you put your predicate in the wrong place in your path expression, or you forgot to add some parentheses. Consider this input file <code>doc.txt</code>:</p>
<pre class="cpp">

  &lt;doc&gt;
    &lt;p&gt;
      &lt;span&gt;1&lt;/span&gt;
      &lt;span&gt;2&lt;/span&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;span&gt;3&lt;/span&gt;
      &lt;span&gt;4&lt;/span&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;span&gt;5&lt;/span&gt;
      &lt;span&gt;6&lt;/span&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;span&gt;7&lt;/span&gt;
      &lt;span&gt;8&lt;/span&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;span&gt;9&lt;/span&gt;
      &lt;span&gt;a&lt;/span&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;span&gt;b&lt;/span&gt;
      &lt;span&gt;c&lt;/span&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;span&gt;d&lt;/span&gt;
      &lt;span&gt;e&lt;/span&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;span&gt;f&lt;/span&gt;
      &lt;span&gt;0&lt;/span&gt;
    &lt;/p&gt;
  &lt;/doc&gt;

</pre>
<p>Suppose you want the first <code>&lt;span&gt;</code> element of every <code>&lt;p&gt;</code> element. Apply a position filter (<code>[1]</code>) to the <code>/span</code> path step:</p>
<pre class="cpp">

  let $doc := doc('doc.txt')
  return $doc/doc/p/span[1]

</pre>
<p>Applying the <code>[1]</code> filter to the <code>/span</code> step returns the first <code>&lt;span&gt;</code> element of each <code>&lt;p&gt;</code> element:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>span<span class="operator">&gt;</span><span class="number">1</span><span class="operator">&lt;</span><span class="operator">/</span>span<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>span<span class="operator">&gt;</span><span class="number">3</span><span class="operator">&lt;</span><span class="operator">/</span>span<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>span<span class="operator">&gt;</span><span class="number">5</span><span class="operator">&lt;</span><span class="operator">/</span>span<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>span<span class="operator">&gt;</span><span class="number">7</span><span class="operator">&lt;</span><span class="operator">/</span>span<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>span<span class="operator">&gt;</span><span class="number">9</span><span class="operator">&lt;</span><span class="operator">/</span>span<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>span<span class="operator">&gt;</span>b<span class="operator">&lt;</span><span class="operator">/</span>span<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>span<span class="operator">&gt;</span>d<span class="operator">&lt;</span><span class="operator">/</span>span<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>span<span class="operator">&gt;</span>f<span class="operator">&lt;</span><span class="operator">/</span>span<span class="operator">&gt;</span>

</pre>
<p><b>Note: </b>: You can write the same query this way:</p><pre class="cpp">

  <span class="keyword">for</span> $a in doc(<span class="char">'doc.txt'</span>)<span class="operator">/</span>doc<span class="operator">/</span>p<span class="operator">/</span>span<span class="operator">[</span><span class="number">1</span><span class="operator">]</span>
  <span class="keyword">return</span> $a

</pre>
<p>Or you can reduce it right down to this:</p>
<pre class="cpp">

  doc(<span class="char">'doc.txt'</span>)<span class="operator">/</span>doc<span class="operator">/</span>p<span class="operator">/</span>span<span class="operator">[</span><span class="number">1</span><span class="operator">]</span>

</pre>
<p>On the other hand, suppose you really want only one <code>&lt;span&gt;</code> element, the first one in the document (i.e&#x2e;, you only want the first <code>&lt;span&gt;</code> element in the first <code>&lt;p&gt;</code> element). Then you have to do more filtering. There are two ways you can do it. You can apply the <code>[1]</code> filter in the same place as above but enclose the path expression in parentheses:</p>
<pre class="cpp">

  let $doc := doc('doc.txt')
  return ($doc/doc/p/span)[1]

</pre>
<p>Or you can apply a second position filter (<code>[1]</code> again) to the <code>/p</code> path step:</p>
<pre class="cpp">

  let $doc :<span class="operator">=</span> doc(<span class="char">'doc.txt'</span>)
  <span class="keyword">return</span> $doc<span class="operator">/</span>doc<span class="operator">/</span>p<span class="operator">[</span><span class="number">1</span><span class="operator">]</span><span class="operator">/</span>span<span class="operator">[</span><span class="number">1</span><span class="operator">]</span>

</pre>
<p>Either way the query will return only the first <code>&lt;span&gt;</code> element in the document:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>span<span class="operator">&gt;</span><span class="number">1</span><span class="operator">&lt;</span><span class="operator">/</span>span<span class="operator">&gt;</span>

</pre>
<a name="why-doesn-t-my-flwor-behave-as-expected"></a>
<h3 >Why doesn't my FLWOR behave as expected?</h3>
<p>The quick answer is you probably expected your <a href="xmlprocessing.html">XQuery</a> FLWOR to behave just like a C++ <i>for</i> loop. But they aren't the same. Consider a simple example:</p>
<pre class="cpp">

  for $a in (8, -4, 2)
  let $b := ($a * -1, $a)
  order by $a
  return $b

</pre>
<p>This query evaluates to <i>4 -4 -2 2 -8 8</i>. The <i>for</i> clause does set up a <i>for</i> loop style iteration, which does evaluate the rest of the FLWOR multiple times, one time for each value returned by the <i>in</i> expression. That much is similar to the C++ <i>for</i> loop.</p>
<p>But consider the <i>return</i> clause. In C++ if you hit a <i>return</i> statement, you break out of the <i>for</i> loop and return from the function with one value. Not so in <a href="xmlprocessing.html">XQuery</a>. The <i>return</i> clause is the last clause of the FLWOR, and it means: <i>Append the return value to the result list and then begin the next iteration of the FLWOR</i>. When the <i>for</i> clause's <i>in</i> expression no longer returns a value, the entire result list is returned.</p>
<p>Next, consider the <i>order by</i> clause. It doesn't do any sorting on each iteration of the FLWOR. It just evaluates its expression on each iteration (<code>$a</code> in this case) to get an ordering value to map to the result item from each iteration. These ordering values are kept in a parallel list. The result list is sorted at the end using the parallel list of ordering values.</p>
<p>The last difference to note here is that the <i>let</i> clause does <i>not</i> set up an iteration through a sequence of values like the <i>for</i> clause does. The <i>let</i> clause isn't a sort of nested loop. It isn't a loop at all. It is just a variable binding. On each iteration, it binds the <i>entire</i> sequence of values on the right to the variable on the left. In the example above, it binds (4 -4) to <code>$b</code> on the first iteration, (-2 2) on the second iteration, and (-8 8) on the third iteration. So the following query doesn't iterate through anything, and doesn't do any ordering:</p>
<pre class="cpp">

  let $i := (2, 3, 1)
  order by $i[1]
  return $i

</pre>
<p>It binds the entire sequence (2, 3, 1) to <code>$i</code> one time only; the <i>order by</i> clause only has one thing to order and hence does nothing, and the query evaluates to 2 3 1, the sequence assigned to <code>$i</code>.</p>
<p><b>Note: </b>We didn't include a <i>where</i> clause in the example. The <i>where</i> clause is for filtering results.</p><a name="why-are-my-elements-created-in-the-wrong-order"></a>
<h3 >Why are my elements created in the wrong order?</h3>
<p>The short answer is your elements are <i>not</i> created in the wrong order, because when appearing as operands to a path expression, there is no correct order. Consider the following query, which again uses the input file <code>doc.txt</code>:</p>
<pre class="cpp">

  doc(<span class="char">'doc.txt'</span>)<span class="comment">//p/&lt;p&gt;{span/node()}&lt;/p&gt;</span>

</pre>
<p>The query finds all the <code>&lt;p&gt;</code> elements in the file. For each <code>&lt;p&gt;</code> element, it builds a <code>&lt;p&gt;</code> element in the output containing the concatenated contents of all the <code>&lt;p&gt;</code> element's child <code>&lt;span&gt;</code> elements. Running the query through <code>xmlpatterns</code> might produce the following output, which is not sorted in the expected order.</p>
<pre class="cpp">

  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">78</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">9a</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">12</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span>bc<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span>de<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">34</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">56</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span>f0<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>

</pre>
<p>You can use a <i>for</i> loop to ensure that the order of the result set corresponds to the order of the input sequence:</p>
<pre class="cpp">

  <span class="keyword">for</span> $a in doc(<span class="char">'doc.txt'</span>)<span class="comment">//p</span>
   <span class="keyword">return</span> <span class="operator">&lt;</span>p<span class="operator">&gt;</span>{$a<span class="operator">/</span>span<span class="operator">/</span>node()}<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>

</pre>
<p>This version produces the same result set but in the expected order:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">12</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">34</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">56</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">78</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span><span class="number">9a</span><span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span>bc<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span>de<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>p<span class="operator">&gt;</span>f0<span class="operator">&lt;</span><span class="operator">/</span>p<span class="operator">&gt;</span>

</pre>
<a name="why-can-t-i-use-keyword-true-keyword-and-keyword-false-keyword-in-my-xquery"></a>
<h3 >Why can't I use <code>true</code> and <code>false</code> in my XQuery?</h3>
<p>You can, but not by just using the names <code>true</code> and <code>false</code> directly, because they are <a href="xquery-introduction.html#name-tests">name tests</a> although they look like boolean constants. The simple way to create the boolean values is to use the builtin functions <code>true()</code> and <code>false()</code> wherever you want to use <code>true</code> and <code>false</code>. The other way is to invoke the boolean constructor:</p>
<pre class="cpp">

  xs:boolean(&quot;true&quot;)

</pre>
</div>
<!-- @@@xquery-introduction.html -->
<p class="naviNextPrevious footerNavi">
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>