medic-enketo-xslt
v1.5.0
Published
XSL stylesheets for the Enketo XForm transformation to be run client-side in a web browser
Downloads
44
Readme
Enketo XSLT
XSLT sheets used to transform OpenRosa-flavoured XForms into an HTML form and XML instance.
This is a fork of enketo-xslt
, modified to run client-side, in a web browser.
Browser Support
We commonly test on:
- Firefox
- Chrome
Examples
Code
(This example uses jQuery to parse strings into XMLDocument
instances.)
var htmlTransformXsltAsString = // loaded somehow
var modelTransformXsltAsString = // loaded somehow
var htmlTransformXslt = jQuery.parseXML(htmlTransformXsltAsString);
var modelTransformXslt = jQuery.parseXML(modelTransformXsltAsString);
var xformXmlAsString = // loaded somehow
var xformXml = jQuery.parseXML(xformXmlAsString);
function transform(xslt, xml) {
var p = new XSLTProcessor();
p.importStylesheet(xslt);
var transformed = p.transformToDocument(xml);
var root = transformed.documentElement.firstElementChild;
return new XMLSerializer().serializeToString(root);
}
var formHtml = transformed(htmlTransformXslt, xformXml);
var formModel = transformed(modelTransformXslt, xformXml);
src/
There is a fuller example in this repository at examples/index.js
.
In the wild
You can find examples of this code in use in medic-webapp
and enketo-collect
.
Use
Use with npm
npm install medic-enketo-xslt
Use without npm and in any programming language.
Use as git submodule and read the two .xsl files as required. Ignore index.js.
License
See license document.
In addition, any product that uses enketo-xslt is required to have a "Powered by Enketo" footer, according to the specifications below, on all screens in which the output of enketo-xslt, or parts thereof, are used, unless explicity exempted from this requirement by Enketo LLC in writing. Partners and sponsors of the Enketo Project, listed on https://enketo.org/#about and on https://github.com/enketo/enketo-core#sponsors are exempted from this requirements and so are contributors listed in package.json.
The aim of this requirement is to force adopters to give something back to the Enketo project, by at least spreading the word and thereby encouraging further adoption.
Specifications:
- The word "Enketo" is displayed using Enketo's logo.
- The minimum font-size of "Powered by" is 12 points.
- The minimum height of the Enketo logo matches the font-size used.
- The Enketo logo is hyperlinked to https://enketo.org
Example:
Powered by
Change Log
See change log
XSL Changes
To suport XSL transformations in browser, we must make a few changes to the upstream stylesheets:
- change XSL version from 2.0 to 1.0
- change
<xsl:message>
calls to<xsl:comment>
- change
x != y
comparisons tonot(x = y)
- only use
str:tokenize
within a feature switch (not supported on all browsers) - work around some unclear browser weirdness relating to
<xsl-apply-templates>
And for our own reasons, we make the following changes:
- Do not include `No Title when we did not set a title in the XForm
- add explicit support for
tel
data type, to allow nice soft-keyboard support on touchscreen devices - add support for unknown data types (treat them all as
string
/text
)
It would also be nice to minify the XSL files before use in production, to minimise transmission size. This could include stripping meaningless whitespace, and XML comments. However, there are some inline comments in the stylesheets which may be required to preserve empty tags in place of self-closing ones.
Feature differences from upstream
The above changes necessitate some differences in features from the standard enketo-xslt
lib lib:
- only a single
or-appearance
can be applied to a particular input node. jr:constraintMsg
andjr:requiredMsg
cannot have dynamic content