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

<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!