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.


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.


The guiding goals for the RESTXQ specification are:

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.


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 one of two namespaces (see [[!XML-NAMES]]) and referenced using an xs:QName. The namespace prefix used in this document for annotations and functions that are solely related to RESTXQ is rest. Serialization annotations and functions are named with the prefix output.

This document uses the prefix rerr to represent the namespace URI, which is the namespace for all RESTXQ error codes and messages. This namespace prefix is not predeclared and its use in this document is not normative.

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

The URIs of the namespaces and the default prefixes associated with them are:

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.



RESTXQ is heavily influenced by JAX-RS [[!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:


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.


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 a base URI (), which is implementation-defined.

The URI path itself 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 should be performed at run-time, and an error should be raised if conversion is impossible.

            function local:widget($id as xs:int) {
              (: get the widget :)
              fn:collection("/db/widgets")/widget[@id eq $id]

In the above example, a HTTP GET on the following URI would cause the widget with the id of '1981' to be retrieved:{$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.

            function local:widget($id as xs:int) {
              (: deletes a widget :)
              delete node fn:collection("/db/widgets")/widget[@id eq $id]

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. Cardinality that allows for one or more of the typed item(s).
  2. Typing that is 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 is textual, the function parameter type will be xs:string.
    2. If the media-type is an XML media-type, the request body is parsed as XML and the function parameter type will be document-node().
    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 types such as HTML or JSON.

Consumes Annotation

Resource Functions MAY be constrained to certain media-types 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 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 content types :)
              %rest:consumes("application/xml", "application/atom+xml")
            function local: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 same also make use of the template syntax to map the 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. Cardinality that allows 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 should be raised by the implementation.
  2. Type that inherits from xs:anyAtomicType, otherwise, an error should be raised by the implementation. In addition, conversion from the parameter string to the required type should be performed at run-time, and an error raised if conversion is impossible.

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.

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

The annotation %rest:form-param is provided for accessing parameters from an HTML form submitted in the request body of the RESTful Web Service request with the Internet media-type application/x-www-form-urlencoded.

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

The annotation %rest:header-param is provided for accessing HTTP Request headers from the RESTful Web Service request. 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.

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



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.
    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.
  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. The rules of both (1) and (2) must be applied to the result sequence.

REST 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.

          <http:response status?="integer" reason?="string">
          <http:header name="string" value="string"/>

Should the status be omitted for the response, or a REST Response document not 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 those values set in the REST Response document.

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.()

EXPath Package Considerations

HTTP Request Matching

It is often the case that there is one or more Resource Functions that could service an incoming HTTP Request. To avoid conflicts, the most specific Resource Function MUST be applied to an incoming request. This chapter defines the specificity of Resource Functions against HTTP requests.

The specificity of a Resource Function is governed by three rules which MUST be applied in order:

Resource Function 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
local:function-1() {

is more specific than:

declare function
local:function-2() {

Path Preference

More than one Path from a Resource Function Path Annotations MAY appear to satisfy a request, however often these can be disambiguated by specificity of application to the HTTP Request URI. The most specific paths MUST be 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:


Media Type Preference

More than one Media Type from Resource Function Consumes or Produces Annotations MAY appear to satisfy a request, however 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 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() 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:


The rest:resource XML element has the following structure:

  xquery-uri = xs:anyURI>
    namespace = xs:anyURI
    local-name = xs:NCName
    arity = xs:int/>

URI Functions

URI functions assist in the construction and selection of URIs.


rest:base-uri() as xs:anyURI

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


rest:uri() as xs:anyURI

Summary: This function is 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 "}"


Many thanks to: