XQuery/HelloWorld

MotivationEdit

You want to run a small program that tests to see if your XQuery execution environment is working.

XML OutputEdit

xquery version "1.0";
let $message := 'Hello World!'
return
<results>
   <message>{$message}</message>
</results>

Execute

Expected OutputEdit

<?xml version="1.0" encoding="UTF-8"?>
<results>
   <message>Hello World!</message>
</results>

DiscussionEdit

The program creates a temporary variable called $message and assigns it a string value. The output is an XML element containing a message element which contains the value of the variable.

SuggestionsEdit

Try omitting the curly braces from inside of the result message element. What do you get? [Execute]

What happens if you omit the results wrappers? [Execute]

Plain TextEdit

You can get XQuery to return plain text using serialization options which define the serialization and the output media-type.

For example to output the message as text, specify the serialization as text and the media-type as text/plain.

xquery version "1.0";
declare option exist:serialize "method=text media-type=text/plain";
let $message := 'Hello World!'
return
$message

[Execute]

Expected OutputEdit

Depending on your browser set-up, this will launch a viewer for text documents and display

Hello World!

Execution MethodsEdit

If you are using the oXygen IDE this can be done by selecting the "transform" icon on the toolbar.

If you are running this program in the eXist databases you can upload a file called hello.xq using the "Browse" function in the web administrator and then run the following in the browser:

http://localhost:8080/exist/rest/db/hello.xq

There are three important items to note in this URL.

  1. This is the URL that you would use if you used the default eXist configuration
  2. Note that the world "rest" is in the URL before the "/db" indicating that you are using the REST interface (as opposed to the WebDAV, Atom or SOAP interface)
  3. Note that the port is "8080" (the default port for development web sites) and that the "context" of the server is "exist". Both of these can be easily changed by editing the $EXIST_HOME/tools/jetty/etc/conf.xml file and restarting your eXist server. The short-form on production sites might be:

http://localhost/rest/db/hello.xq

With tools like URL rewriting you can also remove the "/rest" and the "/db" components of the URL.

Last modified on 22 October 2013, at 03:26