XForms/eXist

eXist is a native XML database that has a full REST interface. This allows you to use eXist to save form data without writing any middleware glue.

eXist REST interface
eXist provides a full REST interface that is easy to call from an XForm.

eXist WebDAV interface
eXist also provides a full WebDAV interface. This allows you to do HTTP "PUT" operations directly from your forms.

eXist Example
In this example, we will create a user named "juser" (Jane User) and setup a collection to read and write form data.

Sample Program
To run this program, just install eXist from the web site. use the eXist admin menu to create a user called "juser". Then create a home collection for juser at /db/home/juser. Then login as juser and create a collection called "test". You will put a form and instance data into this file.

To read and write instance data into a form just use the following example:

Note that if the data is in the same collection as the form (not a good long term design) you can use a relative path reference. Also note that we are doing an HTTP PUT operation to the webdav (not rest) interface to exist.

Using eXist URLs
It is easy to load XML data directly into your XForms application. In the instance statement in your model just add a src attribute and put a path name to the file you want to edit. The key is to put the word "rest" in the URL when you are reading data into your form:



Always put the word "webdav" in the URL path when doing an HTTP PUT.

http://localhost:8080/exist/ webdav/db/home/juser/test/data.xml

That is all there is to it!

For more information see the eXist manual on using the REST and WebDAV interfaces for eXist.

Using XQuery to return Document IDs
When using an XML database, you sometimes wish to have the database receive an XML document and store it in the correct location based on business rules such as the content of an XML document and the date. This can be done in relational databases using stored procedures.

XQuery can be thought of as an XML-oriented stored procedure. eXist allows you to save directly to the URL of an XQuery and the XQuery can store the document based on a number of rules.

Here is an example eXist XQuery that that takes HTTP POST and creates a path name to a collection based on the county in input document and the date. After it saves the file and checks that the save has worked it increments a counter similar to the AUTOINCREMENT property of some relational databases.

Processing the Results
This XQuery returns an XML message such as the following

Assuming you have a hostname, port and application-name variables are set up, the XForms application would contain the following code:

Setting the mime type for XQuery Results
If you want your XQuery to return the correct mime type put the following in the preamble:

(This suggestion from Kurt Cagle)