joewiz
11/23/2014 - 4:04 AM

A starter template for a RestXQ module

A starter template for a RestXQ module

xquery version "3.0";

module namespace my-app = "http://my/app";

(: A starter template for a RestXQ module.
    Save this file into eXist anywhere (e.g., /db/restxq-template.xqm or /db/apps/my-app/modules/restxq-template.xqm),
    And access at http://localhost:8080/exist/restxq/index.html.
    (Why? Because /restxq is the URL space for RestXQ, and the function's %rest:path annotation is for requests to "/index.html".) 
    Note that the collection configuration file where you store the module must invoke the RestXQ trigger:
        <collection xmlns="http://exist-db.org/collection-config/1.0">
            <triggers>
                <trigger class="org.exist.extensions.exquery.restxq.impl.RestXqTrigger"/>
            </triggers>
        </collection>
    But this should already be in place, since /db/system/config/db/collection.xconf will apply to any collection
    unless overridden by another collection.xconf file.
:)

declare namespace rest = "http://exquery.org/ns/restxq";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare
    %rest:GET
    %rest:path("/index.html")
    %output:media-type("text/html")
    %output:method("html5")
function my-app:hello() {
    let $title := 'Hello world!'
    return
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title>{$title}</title>
            </head>
            <body>
                <h1>{$title}</h1>
            </body>
        </html>
};