XQuery/HelloWorld

From Wikibooks, the open-content textbooks collection

< XQuery
Jump to: navigation, search

Contents

[edit] Motivation

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

[edit] Example Program

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

Execute

[edit] Expected Output

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

[edit] Discussion

This is a simple program that creates a temporary variable called $message and assigns it a string value.

It then returns an XML file that is enclosed in the results elements. The results have a single element of message and message just contains the value of the message variable.

[edit] Suggestions

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]

[edit] Returning Plain Text

You can get XQuery to return almost any type of document such as plain text, HTML, XML or csv files. This can be done by changing the serialization options. This changes the mime-type in the HTML header that is sent to the browser.

The following returns a file that is not valid XML since it does not have a root element.

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

[Execute]

Note that the default output is text/xml. To create text files, the script must specify text as the output - see Serialisation

[edit] Results

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

Hello World!