<feed xmlns="http://www.w3.org/2005/Atom">
    <id>urn:uuid:c4a74a0f-6c37-4f74-a250-a22e5fd3af64</id>
    <updated>2012-01-17T09:09:41+01:00</updated>
    <title>Switch Expression</title>
    <author>
        <atom:name xmlns:atom="http://www.w3.org/2005/Atom">ljo</atom:name>
    </author>
    <atom:category xmlns:atom="http://www.w3.org/2005/Atom" scheme="http://exist-db.org/NS/wiki/type/" term="wiki"/>
    <link href="#" rel="edit" type="application/atom+xml"/>
    <link href="#" rel="self" type="application/atom+xml"/>
    <atom:entry xmlns:atom="http://www.w3.org/2005/Atom">
        <atom:id>35e280da-3631-41a4-895c-b156f1b86232</atom:id>
        <wiki:id xmlns:wiki="http://exist-db.org/xquery/wiki">XQ3Notes</wiki:id>
        <wiki:editor xmlns:wiki="http://exist-db.org/xquery/wiki">html</wiki:editor>
        <wiki:is-index xmlns:wiki="http://exist-db.org/xquery/wiki">true</wiki:is-index>
        <atom:published>2012-04-09T15:59:52.211+02:00</atom:published>
        <atom:updated>2012-04-09T16:17:06.841+02:00</atom:updated>
        <atom:author>
            <atom:name>editor</atom:name>
        </atom:author>
        <atom:title>XQuery 3.0 Implementation Notes</atom:title>
        <atom:content type="xhtml">
            <article xmlns="http://www.w3.org/1999/xhtml">
                <p>The XQuery 3.0 specification introduces a number of additions to the language. eXist-db 2.0 already supports most of the new language constructs.</p>
                <p>To use XQuery 3.0 features, your query or module must start with an XQuery declaration specifying version "3.0":</p>
                <pre class="codeblock ext:code?lang=xquery">xquery version "3.0";</pre>
                <p>The following notes cover selected features:</p>
                <ul>
                    <li>
                        <a href="SwitchExpressionExample">Switch Expression</a>: a switch on atomic values</li>
                    <li>
                        <a href="Try-CatchExpression">Try-Catch Expressions</a>: error handling</li>
                    <li>
                        <a href="GroupByClause">Group by clauses</a> in FLWOR expressions: eXist had an extension for group by clauses since 2006. It has not been aligned with the XQuery 3.0 specs yet, but is nevertheless very useful.</li>
                </ul>
            </article>
        </atom:content>
        <atom:link type="blog" href="/db/apps/wiki/data/HowTo/XQuery3"/>
    </atom:entry>
    <entry>
        <id>urn:uuid:768115e7-00e4-4da5-be7c-5daeeb0fef49</id>
        <updated>2011-03-17T17:57:34+01:00</updated>
        <atom:published xmlns:atom="http://www.w3.org/2005/Atom">2011-03-13T12:33:31+01:00</atom:published>
        <link href="?id=urn:uuid:768115e7-00e4-4da5-be7c-5daeeb0fef49" rel="edit" type="application/atom+xml"/>
        <title type="text">Group By Clause</title>
        <wiki:id xmlns:wiki="http://exist-db.org/xquery/wiki">GroupByClause</wiki:id>
        <author>
            <atom:name xmlns:atom="http://www.w3.org/2005/Atom">ljo</atom:name>
        </author>
        <wiki:lock xmlns:wiki="http://exist-db.org/xquery/wiki" user="dannes"/>
        <atom:content xmlns:atom="http://www.w3.org/2005/Atom" type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>To make the awareness of the longtime addition of the Group by Clause supplied by Boris Verhaegen already in November 2006 to eXist-db bigger, I give you Boris's short but clean example of its use. This posting is the first step in order to close bug #3165906 about documenting this feature.</p>
                <div class="ext:code?lang=xquery">
let $g-b-data := &lt;items&gt;
    &lt;item&gt;
        &lt;key1&gt;1&lt;/key1&gt;
        &lt;key2&gt;a&lt;/key2&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;key1&gt;1&lt;/key1&gt;
        &lt;key2&gt;b&lt;/key2&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;key1&gt;0&lt;/key1&gt;
        &lt;key2&gt;c&lt;/key2&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;key1&gt;0&lt;/key1&gt;
        &lt;key2&gt;d&lt;/key2&gt;
    &lt;/item&gt;
&lt;/items&gt;

(: grouping query :)
return
for $item in $g-b-data//item
group $item as $partition by $item/key1 as $key1
return
&lt;group&gt;
  {$key1,$partition}
&lt;/group&gt;

</div>
                <p>Which gives the following result:</p>
                <div class="ext:code?lang=xml">
&lt;group&gt;
    &lt;key1&gt;1&lt;/key1&gt;
    &lt;item&gt;
        &lt;key1&gt;1&lt;/key1&gt;
        &lt;key2&gt;a&lt;/key2&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;key1&gt;1&lt;/key1&gt;
        &lt;key2&gt;b&lt;/key2&gt;
    &lt;/item&gt;
&lt;/group&gt;
&lt;group&gt;
    &lt;key1&gt;0&lt;/key1&gt;
    &lt;item&gt;
        &lt;key1&gt;0&lt;/key1&gt;
        &lt;key2&gt;c&lt;/key2&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;key1&gt;0&lt;/key1&gt;
        &lt;key2&gt;d&lt;/key2&gt;
    &lt;/item&gt;
&lt;/group&gt;
</div>
            </div>
        </atom:content>
        <atom:link xmlns:atom="http://www.w3.org/2005/Atom" type="blog" href="/db/apps/wiki/data/HowTo/XQuery3"/>
    </entry>
    <entry>
        <id>urn:uuid:70a9285e-5d4a-4692-a9e5-e276ff150827</id>
        <updated>2011-10-25T22:08:20+02:00</updated>
        <atom:published xmlns:atom="http://www.w3.org/2005/Atom">2011-03-13T12:31:41+01:00</atom:published>
        <link href="?id=urn:uuid:70a9285e-5d4a-4692-a9e5-e276ff150827" rel="edit" type="application/atom+xml"/>
        <title type="text">Switch Expression</title>
        <wiki:id xmlns:wiki="http://exist-db.org/xquery/wiki">SwitchExpressionExample</wiki:id>
        <author>
            <atom:name xmlns:atom="http://www.w3.org/2005/Atom">dizzzz</atom:name>
        </author>
        <atom:content xmlns:atom="http://www.w3.org/2005/Atom" type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>The Switch Expression is a welcome <a href="http://www.w3.org/TR/xquery-30/#id-switch "> addition</a> to the XML Query Language. This example presents its potential. </p>
                <div class="ext:code?lang=xquery">
xquery version "3.0";
let $animal := "Cat"
return
 switch ($animal) 
   case "Cow" case "Calf" return "Moo"
   case "Cat" return "Meow"
   case "Duck" return "Quack"
   default return "What's that odd noise?"
</div>
                <p>returns <div class="ext:code">Meow</div>
                </p>
                <p>Note the sequence of switch case clauses <div class="ext:code?lang=xquery">
case "Cow" case "Calf" return "Moo"
</div> sharing the same return expression.</p>
                <p>
                    <em>The expression is available starting eXist-db 1.5</em>
                </p>
            </div>
        </atom:content>
        <atom:link xmlns:atom="http://www.w3.org/2005/Atom" type="blog" href="/db/apps/wiki/data/HowTo/XQuery3"/>
    </entry>
    <entry>
        <id>urn:uuid:90f7a15c-778f-4582-ae78-9f54dec73eca</id>
        <updated>2012-01-17T09:09:41+01:00</updated>
        <atom:published xmlns:atom="http://www.w3.org/2005/Atom">2011-03-13T12:29:53+01:00</atom:published>
        <link href="?id=urn:uuid:90f7a15c-778f-4582-ae78-9f54dec73eca" rel="edit" type="application/atom+xml"/>
        <title type="text">Try-Catch Expression</title>
        <wiki:id xmlns:wiki="http://exist-db.org/xquery/wiki">Try-CatchExpression</wiki:id>
        <author>
            <atom:name xmlns:atom="http://www.w3.org/2005/Atom">admin</atom:name>
        </author>
        <atom:content xmlns:atom="http://www.w3.org/2005/Atom" type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>The Try-Catch Expression is another welcome <a href="http://www.w3.org/TR/xquery-30/#id-try-catch "> addition</a> to the XML Query Language. This page introduces the expression and provides some examples.</p>
                <h2>XQuery 1.0</h2>
                <p>While the first version of the xquery language provided the possibility to raise errors, the language had no standard mechanism to handle these errors. The following two queries show how it works:</p>
                <div class="ext:code?lang=xquery">
xquery version '1.0';
7 + "a"
</div>
                <p>results in <code>"XPTY0004: <a href=""/> can not be an operand for +"</code>
                </p>
                <div class="ext:code?lang=xquery">
xquery version '1.0';
xmldb:store('/db/not-existent/', 'test.xml', &lt;a/&gt;)
</div>
                <p>results into <code>"Could not locate collection: /db/not-existent/"</code>
                </p>
                <p>For a long time eXist-db provided a custom mechanism to handle these errors with the <code>
                        <a href=""/>
                    </code> function. An example: </p>
                <div class="ext:code?lang=xquery">
xquery version '1.0';
util:catch( "*", 7 + "a" , "handle error" )
</div>
                <p>Additional information about the error can be obtained from two variables which are set by the function:</p>
                <div class="ext:code">
$util:exception , $util:exception-message
</div>
                <p>For more details check the eXist-db function <a href="http://demo.exist-db.org/exist/functions/util/catch "> documentation</a>.</p>
                <h2>XQuery 3.0</h2>
                <p>In version 3.0 of XQuery language the handling of errors has become part of the language: the try-catch expression. An introduction example:</p>
                <div class="ext:code?lang=xquery">
xquery version '3.0';
try {
   (try expression)
} catch (catch error list or "*") {
   (catch expression)
}
</div>
                <p>The "catch error list" specifies wich errors are handled in the next catch expression; the asterix wildcard is an special example meaning all errors match.</p>
                <p>It is possible to have more catch clauses, the values in the catch-error-list determines which catch block is executed.</p>
                <p>The following query is a nice complete example of the try-catch expression. The outcome of the query is <code>2</code>:</p>
                <div class="ext:code?lang=xquery">
xquery version '3.0';
try { 'a' + 7 } 
catch err:XPTY0001 | err:XPTY0002 
{ 1 } 
catch err:XPTY0004 
{ 2 } 
catch err:XPDY0003 | err:XPDY0005 
{ 3 } 
catch * 
{ 4 } 
</div>
                <p>The java exception of the second example can be handled as well:</p>
                <div class="ext:code?lang=xquery">
xquery version '3.0';
try {
    xmldb:store('/db/not-existent/', 'test.xml', &lt;a/&gt;)

} catch java:org.xmldb.api.base.XMLDBException {
  "b"
}
</div>
                <p>Finally it is possible to raise and handle an xquery error using the <a href=""/> function:</p>
                <div class="ext:code?lang=xquery">
xquery version '3.0';
try {
fn:error( fn:QName('http://www.w3.org/2005/xqt-errors', 'err:FOER0000') ) 
} catch err:FOER0000 {
  $err:code , $err:description, $err:value 
}
</div>
                <p>For more details check the examples in the <a href="http://www.w3.org/TR/xquery-30/#id-try-catch "> specification</a> and in the corresponding <a href="http://www.w3.org/TR/xquery-30-use-cases/#try-catch-use-cases "> usecases</a>. These examples are integrated into our test suite.</p>
                <p>
                    <em>The expression is available starting eXist-db 1.5 ; in Jan 2012 changes were made to reflect the latest specification</em>
                </p>
            </div>
        </atom:content>
        <atom:link xmlns:atom="http://www.w3.org/2005/Atom" type="blog" href="/db/apps/wiki/data/HowTo/XQuery3"/>
    </entry>
</feed>