JavaScript XPath Evaluator for OpenRosa

Martijn (admin), 21 Aug 2012

After finding a suitable open-source form format to use in Enketo there was need to properly evaluate the very powerful processing logic it supports. For example, the format allows you to describe how one survey question depends on the answer to an earlier question. This logic is written in a language called XPath and I therefore needed an XPath Evaluator. Since Enketo is offline-capable this processor would have to work without server interaction inside a browser. This meant I could not use the open-source Java libraries provided by the OpenRosa project.

Initially, I had high hopes for the (crazy fast) native XPath evaluator built into modern browsers. However, it became apparent that it is not really possible to extend this with custom functions. Moreover, browsers do not all implement XPath in the same way (sigh…). XPath is an extensible language and has been extended with useful functions in OpenRosa, so extensibility was crucial. Eventually, I found an excellent cross-browser open-source project called XPathJS by Andrej Pavlovic (Andrej, thank you for making this available!). I forked his project and simply added the custom functions and the custom (date) datatype that OpenRosa has included in their format.

xpathjs_openrosa github screenshot

My port is now available on GitHub and is called enketo-XPathJS. Hopefully, this project will contribute to the creation of more OpenRosa clients for the browser and allow the format to be adopted on a wider scale. I think it is a great thing for organizations when they can easily swap different components (data server, data entry client, form builder) of their information management system. The ability to switch to something new when it becomes available and may meet requirements better – and even to do this on a per-survey basis – is a great advantage compared to systems using proprietary formats and communication protocols. On this note, it is worth mentioning that formhub just added a feature to allow survey administrators to post data to an external site, which supports this ability to swap and try out new tools. Very cool!