XForms

This page is part of the Developer Section and belongs to the Web Application stack. It describes how to use XForms with BaseX.

XForms provides mechanisms to display and edit the contents of XML snippets in the browser without resorting to JavaScript. In combination with the RESTXQ API and the database backend, this is an elegant way of building web applications that completely reside in the XML stack.

Internals
If an HTML document with XForms elements is requested, a web form is generated, which allows users to edit the contents of an XML file. The data model stays consistent during this process, i.e. the data types are always as described in the models. Actions can be configured as well: the model can e. g. be sent to a server and processed further, using the REST or RESTXQ APIs.

Run the example
Several implementations of the XForms Recommendation are available (some AJAX-based, some client-side). In this article, we will focus on the light-weight, LGPL-licensed XSLTForms project from Alain Couthures. The following steps are required to get the XForms example running:


 * download the xsltforms.zip example file, which includes the demo page and XSLTForms, and extract its contents to your webapp directory
 * start a BaseX HTTP server
 * open a browser and visit the URL

This is the head section of the XForms demo:   

The processing instruction in the first line tells the browser where to find the XSLTForms implementation. The rules of the XML Stylesheet will then be applied to transform the XForms elements in the document to HTML. As the XForms code is interpreted, it can not be inspected in the browser, but you can press F1 to enter the debug mode.

Usually, the XForms Model is placed in the head section of the HTML document:

   Like A Rolling Stone Bob Dylan 1965-06-21          Folk   

It contains an instance of the model and a binding. The model is some plain XML, and the xf:bind elements can be used to bind elements to a specific type.

The data can be accessed with the  element, and the XML nodes to be displayed are addressed via XPath 1.0 in the ref attribute. For example, the artist is addressed via:



To modify the XML instance,  elements are used. With the following code,



an input element is displayed that allows users to change the date. As  was bound to dates in the data model, a date picker will be presented for choosing a valid date.