Adam Retter

adam@evolvedbinary.com
 

Declarative Amsterdam
@ CWI, Amsterdam
2025-11-06


@adamretter

Building a Web API
with an XML Database

About Me

  • Director of Evolved Binary

    • UK - Software, Consultancy, Training, and R&D

  • Co-founder and Co-owner of eXist Solutions

    • Germany - TEI Publisher software

  • Software Engineer / Prolific Open Source contributor

    • Elemental - Founder

    • eXist-db - 20 Years as Core contributor (last 9 as Main dev.)

    • RocksDB - 7 Years as RocksJava main developer

  • W3C XQuery Working Group - Invited expert

  • Founder of EXQuery, and creator of RESTXQ

  • Enjoys Research and Development, and Snowboarding

Web API with an XML Database

  • Web API

    • REST - Representational State Transfer

    • HATEOAS over HTTP

  • XML Database

"Steven Pemberton mentioned that he has coded a simple Web API for use with his XForms work.

 

I think it would be interesting to look at how Web APIs can be easily delivered on top of an XML database (i.e. Elemental) as they have many of the things you need already baked in.

 

We will look at RESTXQ and constructing an API in XQuery."

So... What do we need first?

 

Requirements!

Steven's Requirements

  1. PUT an XML "instance" to the store

  2. GET an XML instance from the store

  3. "do a HEAD on it"

  4. POST an element, that is appended to the end of an XML instance

Easy enough:

  1. PUT an XML "instance" to the store

  2. GET an XML instance from the store

Hmm, what is this:

  1. "do a HEAD on it"

Resource Metadata:

  • HTTP HEAD Method

    • Retrieves only the HTTP Headers of a Resource. No content!

  • Often includes:

    • Content-Length

    • Last-Modified

    • ETag

  • Do we really need a dedicated HEAD method?

    • Might depend on the Client applications

    • Could just use conditional requests: e.g. If-Modified-Since

Hmm... And what about this...:

  1. POST an element, that is appended to the end of an XML instance

Aside - What is special about APIs for XForms?

  • Steven's application is written in XForms

  • XForms

    • Read/Update/Create Instances

    • An Instance is an XML Document

    • Most XForms implementations can HTTP GET/PUT/POST XML Documents

Do we really need update?

  • XForms can prepare an updated XML document client-side

  • XForms could just PUT the updated document over the top of an existing document

  • Approaches

    • Explicit Update

      1. Create: PUT /my-collection

      2. Retrieve: GET /my-collection/my-document.xml

      3. Update: POST /my-collection/my-document.xml

    • Just Create/Replace and Retrieve

      1. Create: PUT /my-collection

      2. Retrieve: GET /my-collection/my-document.xml

      3. Replace: PUT /my-collection/my-document.xml

How should Update work?

  • REST Semantics for Update are unclear

  • Should you use POST or PATCH?

Steven's Requirements (Refined)

  1. Create a Document

    • HTTP PUT an XML document to the store

  2. Retrieve a Document

    • HTTP GET an XML document from the store

    • Conditionally if modified

  3. Update a Document

    • HTTP PATCH an XML document with modifications to the XML

Adam's Requirements Bonus Points:

  1. List available Documents

    • HTTP GET a list of XML documents available from the store

  2. Replace a Document

    • HTTP PUT an XML document to an existing URI

  3. Delete a Document

    • HTTP DELETE an XML document from the store

  4. JSON Support

    • Provide JSON versions of the API

Requirements ✅


...What should we do first?

Define our API!

Defining our REST API

We will look at 3 different approaches:

  1. Elemental's REST API

  • No Code

  • Served by Elemental's Web Server

  • Using an XML Database in your own Web API code

    • Bespoke Code

    • Any language - we will use Java 25

    • Use a language framework - we will use JAX-RS

    • Served by Micronaut/Jetty

  • Building a Web API directly in the XML Database

    • Bespoke Code

    • Implemented in XQuery

    • Use a language framework - we will use RESTXQ

    • Served by Elemental's Web Server

  • Elemental's REST API

    Can Elemental's REST API meet our requirements?

    1. Create a Document

      • HTTP PUT /exist/rest/db/dasp-documents/my-document.xml

        • Request body is the content for the XML document

        • Need to know document ID in advance!

    2. Retrieve a Document

      • HTTP GET /exist/rest/db/dasp-documents/my-document.xml

        • Conditional: Supports If-Modified-Since request header

    3. Update a Document

      • HTTP POST /exist/rest/db/dasp-documents/my-document.xml

        • Request body is an XUpdate document

    Can Elemental's REST API meet our requirements?

    1. List available Documents

      • HTTP GET /exist/rest/db/dasp-documents

    2. Replace a Document

      • HTTP PUT /exist/rest/db/dasp-documents/my-document.xml

        • Request body is the new content for the XML document

    3. Delete a Document

      • HTTP DELETE /exist/rest/db/dasp-documents/my-document.xml

    4. JSON Support

      • Not without additional bespoke XQuery code!

    Nice!

    That was easy... Are we finished now?

    • ✅ Delivered all 3 mandatory requirements

    • 👍 Delivered 3 of 4 bouns requirements

    • But:

      • API is very generic

    Using an XML Database in your own Web API code

    Building a Web API directly in the XML Database

    IN CONCLUSION

    Conclusions

    • Just Elemental can be enough - No Code

    • Java implementation was easy, should be same for others (e.g. Python, PHP, etc)

    • RESTXQ implementation was simplest, least code

    • In future: Auto-generate RESTXQ code from Open API

    Thanks
    and
    Questions

    Building a Web API with an XML Database

    By Adam Retter

    Building a Web API with an XML Database

    Declarative Amsterdam @ CWI, Amsterdam 2025-11-06

    • 92