XQuery/HelloWorld

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit]

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

XML Output[edit]

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

Execute

Expected Output[edit]

<results>
   <message>Hello World!</message>
</results>

Discussion[edit]

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.

Suggestions[edit]

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 Text[edit]

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 Output[edit]

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

Hello World!