It is unclear yet whether this document will be submitted to the W3C, either to the XQuery WG or as Community Group. For the time being it lives under the EXQuery project.
Whilst XQuery [[!XQUERY]] was originally envisaged and designed as a query language for XML, it has been adopted by many as a language for application development. This specification describes a set of XQuery Annotations [[!XQUERY-30]] and a small set of functions to enable XQuery to provide RESTful services, thus enabling Web Application development in XQuery.

Introduction

XQuery processors are now frequently provided as part of complete data application processing platforms, which typically incorporate amongst others, XML Data storage and Web serving capabilities.

XQuery has long been recognised as a good language for producing XHTML and HTML from complex data queries, however XQuery is almost completely ignorant of the Web. XQuery provides no native capabilities for either making Web requests or operating as a server-side scripting language and processing Web requests.

As of XQuery 3.0, there is still no standard way to create Web Applications in XQuery. Many vendors provide extensions to their XQuery implementations which allow users to serve web requests using XQuery processing. Whilst vendors have borrowed ideas from each other, there is no standard for Web capabilities in XQuery, as such developers have to use proprietary extensions, which limits the portability of their XQuery code, ultimately fragmenting the XQuery community, limiting re-use and reducing peer-learning.

RESTXQ attempts to resolve these problem of interoperablility. RESTXQ defines a standard set of vendor agnostic XQuery Annotations and functions for XQuery. When implemented by vendors, these annotations provide a standard W3C XQuery compliant approach to delivering RESTful Web Services from XQuery, whilst the functions provide user convenience for interacting with implementations of RESTXQ.

Goals

The guiding goals for the RESTXQ specification are:

Interoperability
Only features that are already present in XQuery MUST be applied to create RESTXQ. Any XQuery code that makes use of RESTXQ instead of vendor extensions, is valid XQuery code, and therefore portable. Note that RESTXQ support is required in the XQuery processor to execute resource functions in a Web context.
Simplicity for XQuery developers
XQuery developers MUST NOT have to maintain external or complex code for wiring RESTful services to XQuery functions. Developers should just write standard XQuery.
Vendor Agnostic
RESTXQ MUST NOT assume anything about an implementation above what is defined in XQuery. RESTXQ MUST be equally implementable by any vendor.
Technical improvement
RESTXQ MUST improve on the current approaches, a comprehensive review was undertaken here.

Audience

This specification is intended for both vendors and developers looking to implement RESTXQ in their products, XQuery developers looking to use the RESTXQ features defined in this specification, and individuals wishing to establish the correctness of implementations with respect to the requirements of this specification.

This document assumes that readers already have at least a basic understanding of XQuery, and an understanding of Web technologies and server side scripting.

Namespaces and Prefixes

The annotations and functions discussed in this document are contained in namespaces (see [[!XML-NAMES]]) and referenced using an xs:QName:

The namespace prefix used for the functions, datatypes and errors can vary, as long as the prefix is bound to the correct URI.

The namespace URI associated with the rerr prefix is not expected to change from one version of this document to another. The contents of this namespace may be extended to allow additional errors to be returned.

Annotations

Background

RESTXQ is heavily influenced by [[!JAX-RS]]. However, we simplify and deviate from JAX-RS predominantly due to the language structure differences between Java and XQuery. Where JAX-RS describes Resource Classes and Resources Methods for Java, in XQuery we simply use the term Resource Function; for mapping HTTP calls to XQuery invocation, our unit of granularity is the XQuery function.

Through the use of annotations on functions in XQuery, we declaratively mark-up the HTTP capabilities of a function. To minimise refactoring by developers when adding annotations to existing code, two measures must be respected by implementations:

  1. Implementations of RESTXQ MUST support annotated functions which have additional function parameters which are not annotation mapped, providing the cardinality type of those un-mapped parameters accepts an empty sequence.
  2. Implementors MUST not enforce the order of function parameters. Whether mapped by annotations or not is unimportant, as annotations explicitly name the parameters to which they are mapped.

Resource Functions

A Resource Function is an XQuery function which has been marked up with RESTXQ annotations. These annotations indicate to a processor that when presented with a RESTful web service request, that matches the constraints indicated by the annotations, that the function SHOULD be invoked and the result SHOULD be returned as the result of the service request.

There are two types of Resource Function Annotations described in RESTXQ:

Templates

Some of the RESTXQ Annotations make use of Templates, which allow for the substitution of parameters from the request into the query. The syntax of these templates is very simple and is designed to be familiar to existing XQuery developers, it is expressed in .

The Template appears inside a string literal within the annotation, but the meaning is that the value of the templated substitution MUST be used as the parameter to the named argument of the annotated function.

          {$number-of-cats}
        

In the example above, a parameter given from the template substitution MUST be set for the function argument called 'number-of-cats' on the annotated function.

Resource Function Constraints

Constraints restrict the service requests that a Resource Function MAY process.

Path Annotation

A Path Annotation maps the URI of a RESTful web service to a Resource Function and provides for path templates.

A Resource Function MUST contain a single path annotation. Additional annotations MAY be used to constrain or parameterize the Resource Function.

The path annotation is named %rest:path and takes a single mandatory literal string, which describes the URI path for this service. The URI path is considered relative to an implementation-defined base URI ().

The path string MAY contain zero or more URI templates which denote path segments that MUST map to named function parameters. Parameters addressed by templates in the URI path must meet the following constraints:

  1. The cardinality MUST allow for an atomic value, otherwise an error should be raised by the implementation, i.e., it must not be of type empty-sequence().
  2. The type MUST inherit from xs:anyAtomicType, otherwise, an error should be raised by the implementation.

Conversion from the URI segment string to the required type is performed at run-time, and an error MUST be raised if conversion is impossible.

            declare
              %rest:path("/stock/widget/{$id}")
            function local:widget($id as xs:int) {
              fn:collection("/db/widgets")//widget[@id = $id]
            };
          

In the above example, an HTTP GET on the following URI would cause the widget with the id of '1981' to be retrieved: http://www.widget-factory.com/stock/widget/{$id}.

When many Resource Functions are defined, there can be many Path Annotations. As such, conflicts may occur, the resolution of such conflicts MUST be processed as described in .
Method Annotation

Resource Functions MAY be constrained to zero or more HTTP methods by means of a method annotation. Unless otherwise constrained by a method annotation, the path annotation of a Resource Function applies to all HTTP methods.

Annotations are defined for all HTTP 1.1 methods except TRACE and CONNECT. All methods MAY return resources except for HEAD, which must only return a rest:response element.

            declare
              %rest:DELETE
              %rest:path("/widget/{$id}")
            function local:widget($id as xs:int) {
              delete node fn:collection("/db/widgets")//widget[@id = $id]
            };
          

The method annotations POST and PUT may take an optional string literal which maps the HTTP request body to a named function parameter. The same syntax as that used for URI templates is applied. For example %rest:POST("{$request-body") would inject the request body into the function through the function parameter named 'request-body'. The function parameter for the request body must meet the following constraints:

  1. The cardinality MUST allow for one or more of the typed item(s).
  2. The type MUST be compatible with the request body. The type of the request body is determined by the HTTP Content-Type header and may be constrained by means of the %rest:consumes annotation. The interpretation of the request body is similar to that of the EXPath HTTP Client:
    1. If the Media Type ([[IANA-MEDIA-TYPES]]) indicated by the Content-Type header matches text/* (excluding text/xml), the function parameter type will be xs:string.
    2. If the Media Type indicated by the Content-Type header is of an XML type, the request body is parsed as XML and the function parameter type will be document-node(). At least the Media Types application/xml and text/xml MUST be interpreted as XML types, although implementations MAY support additional XML types.
    3. Otherwise, a binary media type is assumed, and the function parameter type will be xs:base64Binary.
    4. An implementation MAY provide support for other input type mappings such as HTML or JSON.

Consumes Annotation

Resource Functions MAY be constrained to certain Media Type by means of a %rest:consumes annotation. A function will only be invoked if the HTTP Content-Type header of the request matches one of the given Media Types.

Produces Annotation

If the %rest:produces annotation is specified, a function will only be invoked if the HTTP Accept header of the request matches one of the given types.

            (: Will only be invoked if a user supplies the specified media types :)
            declare
              %rest:path("/widgets")
              %rest:consumes("application/xml", "application/atom+xml")
              %rest:produces("application/xml")
            function local:widgets() {
              fn:collection("/db/widgets")/widgets
            };
          

Resource Function Parameters

Parameters to Resource Functions are extracted from the RESTful Web Service request and passed in as additional function parameters. Unlike constraints, parameters are always optional. Resource Function Parameters use the template syntax to map the request parameter onto a function parameter. They may also provide a default value should the parameter not be present in the request. Resource Function Parameters always place the following constraints on the function parameters that they map to:

  1. The cardinality MUST allow for zero or many atomic values in the case of Query, Form or Header parameters, or zero or one atomic value in the case of Cookie parameters, otherwise an error MUST be raised by the implementation.
  2. The type MUST inherit from xs:anyAtomicType, otherwise, an error MUST be raised by the implementation.

Conversion from the request field to the required function parameter type is performed at run-time, and an error is raised if conversion is impossible.

The annotations in this section MUST have two or more arguments:

  1. The first argument represents the name of a parameter expected in the request.
  2. The second argument contains a reference to the Resource Function Parameter.
  3. The remaining arguments form the default value, which is assigned if the expected parameter is not present in the request.

Query Parameters

The annotation %rest:query-param is provided for accessing parameters in the Query String of the URL used for the RESTful Web Service request.

            declare
              %rest:GET
              %rest:path("/widget/{$id}")
              %rest:query-param("client", "{$client}", "unknown")
            function local:widget($id as xs:int, $client as xs:string*) {
              fn:collection("/db/widgets")//widget[@id = $id][@client = $client]
            };
          
Form Parameters

The annotation %rest:form-param is provided for accessing parameters from an HTML form submitted where the HTTP Content-Type header matches the application/x-www-form-urlencoded Media Type.

            declare
              %rest:GET
              %rest:path("/widget/{$id}")
              %rest:form-param("client", "{$client}", "unknown")
            function local:widget($id as xs:int, $client as xs:string*) {
              fn:collection("/db/widgets")//widget[@id = $id][@client = $client]
            };
          
HTTP Header Parameters

The annotation %rest:header-param is provided for accessing HTTP Request headers. If a single header field value contains comma separated values, an implementation MUST extract each value from the comma separated list into an item in the sequence provided to the function parameter.

            declare
              %rest:GET
              %rest:path("/widget/{$id}")
              %rest:header-param("X-Client-Type", "{$client-type}")
            function local:widget($id as xs:int, $client-type as xs:string*) {
              fn:collection("/db/widgets")//widget[@id = $id][@client-type = $client-type]
            };
          

Response

Serialization

The results of a Resource Function may be serialized back to an HTTP response for the RESTful web service response. A Resource Function may return one of three response types:

  1. A Resource, i.e. just content.
  2. HTTP headers, for example acknowledging the request or providing a status code or additional information.
  3. Both HTTP Headers and a Resource
The XQuery Specification states in Section 2.2.4 how the result of a query is serialized. Serialization may be controlled by the use of Output Declarations. In RESTXQ, the following rules are applied:
  1. If the function is from within a Main Module, and if an output declaration exists, then we use this as the default serialization settings for each Resource Function in that module.
  2. Output Declarations may be re-written as annotations on any Resource Function, e.g. %output:method("xml"). These annotation output declarations override any defaults from 1.
  3. If no Output Declaration, annotated or otherwise, is provided, then the default is to serialize as XML, UTF-8 encoding, with indenting.
Each of the three possible result types of a Resource Function needs to be handled in a different manner by an implementation, and as such we provide appropriate function signature restrictions, and detail how annotations interact with these:
  1. For a function that returns just a resource, either:
    1. If the result type is omitted from the function, it is assumed to be document-node(element()) or just element(), and XML Serialization should be applied to the result of the function. The annotation %output:method, if present, MUST be set to xml. This is known as the Default Resource Response rule.
    2. If the result type is present, it MUST be a type which is compatible with the chosen serialization method, defined by either the XQuery output declaration or overridden by the %output:method annotation on the Resource Function. The default serialization method is XML. If the result type is not compatible with the serialization, an implementation MUST throw an error. This is known as the Typed Resource Response rule.
  2. For a function that returns just HTTP headers, the result type of the function MUST be defined as document-node(element(rest:response)). Any other annotations that effect the serialization of the result are ignored.
  3. For a function that returns both HTTP headers and a resource, the result type of the function MUST be defined as item()+. The first item in the result sequence is the HTTP headers i.e. document-node(element(rest:response), the second item in the result sequence is the resource itself; Subsequently both the Default Resource Response rule and the Typed Resource Response rule MUST be applied to the result sequence.

Response Format

A REST Response document may be returned from a function either with or without a Resource. The purpose of this document is to control the REST (in this case HTTP) response sent back to the client of the RESTful web service.

<rest:response>
  (http:response?)
</rest:response>

<http:response status?="integer" message?="string">
  (http:header*)
</http:response>

<http:header name="string" value="string"/>

Should the status be omitted for the response, or should a REST Response document not be returned from a Resource Function, then the status defaults to 200 OK. It is expected that implementations will make use of sane defaults for HTTP headers as part of their HTTP responses, however any default headers MUST be overridable by the values set in the REST Response document.

As an example, the following response can be returned to trigger a client-side redirection:

<rest:response>
  <http:response status="302" message="Temporary Redirect">
    <http:header name="location" value="/new/location"/>
  </http:response>
</rest:response>

HTTP Mechanics

Base URI

The base URI of a Resource Function is implementation-defined. That is to say that an implementation is free to define either statically or dynamically the URI part that appears before the relative URI of any Path Annotation ().

HTTP Request Matching

In many cases, there is more than one Resource Function that could service an incoming HTTP Request. This chapter defines how the best matching Resource Function is selected. The specificity of a Resource Function is governed by the following rules, which MUST be applied in order.

Constraint Preference

Resource Functions that impose the most specific constraints MUST first be selected as candidates to process the HTTP Request.

Most specific constraints first:

  1. Path, Method, Media Type
  2. Path, Method
  3. Path, Media Type
  4. Path
  5. Method, Media Type
  6. Method
  7. Media Type
The following Resource Function:

declare function
  %rest:GET
  %rest:path("/a/b/c")
  %rest:consumes("application/xml")
local:function-1() {
  <fn>1</fn>
};

is more specific than:

declare function
  %rest:GET
  %rest:path("/a/b/c")
local:function-2() {
  <fn>2</fn>
};
          

Path Preference

More than one Path from a Resource Function Path Annotations MAY appear to satisfy a request. Often, these can be disambiguated by specificity of application to the HTTP Request URI. The most specific paths are selected as candidates to process the HTTP Request.

The rules determining Path specifity to a request are:

  1. Path Segment Length
  2. Path X is more specific than path Y if it has more segments.

    /a/b is more specific than /a.
  3. Path Selection
  4. If more than one Path has the same number of segments, the segments of the paths are compared from left to right.

    Path X is more specific than Path Y if the current segment of Y is a template, while the respective segment of X is not.

    /a/b is more specific than /a/{$x}.
    /a/{$x} is more specific than /{$x}/y.

The following example contains six paths sorted by their specifity:

/person/elisabeth
/person/{$name}
/{$type}/elisabeth
/{$type}/{$name}
/person
/{$type}

Media Type Preference

More than one Media Type from Resource Function Consumes or Produces Annotations MAY appear to satisfy a request. Often, these can be disambiguated by specificity. The most specific media types MUST be selected as candidates to process the HTTP Request.

The rules determining media type specifity are:

  1. Absolute before Wildcard
  2. Absolute Media Types are considered more specific than Media Ranges (Media Types with wildcard subtypes)

                  application/xml is more specific than application/*.
                

RESTXQ Function Module

RESTXQ offers a few simple functions to assist with the construction of RESTful Web Services.

Registry Functions

Functions to assist in managing the RESTXQ Registry.

rest:resource-functions()

rest:resource-functions() as document-node(element(rest:resource-functions))

Summary: This function returns an XML document describing the Resource Functions registered with the RESTXQ Registry.

The XML document root element rest:resource-functions structure:

<rest:resource-functions>
  rest:resource-function*
</rest:resource-functions>

The rest:resource XML element has the following structure:

<rest:resource-function
  xquery-uri = xs:anyURI>
  <rest:identity
    namespace = xs:anyURI
    local-name = xs:NCName
    arity = xs:int/>
</rest:resource-function>

URI Functions

URI functions assist in the construction and selection of URIs.

rest:base-uri()

rest:base-uri() as xs:anyURI

Summary: This function returns the implementation-defined base URI () of the Resource Function.

rest:uri()

rest:uri() as xs:anyURI

Summary: This function returns the complete URI that addresses the Resource Function. Typically this is the rest:base-uri() appended with the path from the Path Annotation (if present) of the Resource Function.

Resources for Implementers

If you plan to implement RESTXQ, there is already a set of common abstraction libraries written in Java which should significantly reduce the ammount of effort involved and avoid re-inventing more wheels. You need just implement a few interfaces and adapters. For more information see the EXQuery GitHub page.

Annotation Template Grammar

The grammar used for RESTXQ Templates is expressed in EBNF and re-uses the EQName from the XQuery grammar

[1] Template ::= "{" "$" EQName "}"

Acknowledgements

Many thanks to: