411 REFERENCE · Wikibooks
XQuery/HelloWorld
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Motivation
You want to run a small program that tests to see if your XQuery execution environment is working.
XML Output
xquery version "1.0";
let $message := 'Hello World!'
return
<results>
<message>{$message}</message>
</results>
[/basics/helloWorld.xq Execute]
Expected Output
<results>
<message>Hello World!</message>
</results>
Discussion
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
Try omitting the curly braces from inside of the result message element. What do you get? [/basics/helloWorld-1.xq Execute]
What happens if you omit the results wrappers? [/basics/helloWorld-2.xq Execute]
Plain Text
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
[/basics/helloWorld-3.xq Execute]
Expected Output
Depending on your browser set-up, this will launch a viewer for text documents and display
Hello World!
fr:XQuery/HelloWorld