Introduction to XSLTForms

Alain Couthures and Micah Dubinko
Last updated 2009-11-13

While XForms is a great way to build information-centric web apps, conventional wisdom has held that it falls short when it comes to browser support. A new crop of XForms engines, however, are built on JavaScript and other client-side technologies. In effect, these turn any modern browser, as far back as IE6, into an XForms platform.

For more on XForms development, and why it's important, see the article XRX: Simple, Elegant, Disruptive.

One such engine, XSLTForms, takes standard XForms markup as input converts it into HTML, JavaScript, and CSS that browsers already understand by taking advantage of the XSLT engine already built in to the browser you're using right now. Internally the XForms and parts of the CSS and XML Schema get compiled into optimized JavaScript objects that are manipulated by an included script library. This approach has a few limitations: security restrictions prevent local file saving for instance, but in general this engine provides everything XForms authors expect.

Where can I get it?

XSLTForms is an open source, LGPL licensed, project hosted by SourceForge.net (link), with a separately hosted project page. An older copy ships with MarkLogic server starting with version 4.1.

How do I get started?

The simplest way is to look at some samples online:

  • balance.xml and xf.xhtml show the basic operation of form handling including data submission.

To run XSLTForms on your own pages, write standard XForms+XHTML markup, as described in books or numerous web sites, and include a link to the XSLTForms stylesheet at the top of your document, like this:

<?xml-stylesheet href="/path/to/xsltforms.xsl"
type="text/xsl"?>

Making it work in XQuery is straightforward. Copy the following, based on hello.xhtml, into an XQuery file named hello.xqy, with the XSLTForms files in a local subdirectory named xsltforms, and run it from a MarkLogic Application Server:

xquery version "1.0-ml";
xdmp:set-response-content-type("application/xml"),
<?xml-stylesheet href="xsltforms/xsltforms.xsl"
type="text/xsl"?>,
<?xsltforms-options debug="yes"?>,
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms">
  <head>
    <title>Hello World in XForms</title>
    <xf:model>
      <xf:instance>
        <data xmlns="">
          <PersonGivenName/>
        </data>
      </xf:instance>
    </xf:model>
  </head>
  <body>
     <xf:input ref="PersonGivenName" incremental="true">
       <xf:label>Please enter your first name: </xf:label>
     </xf:input>
     <br />
     <xf:output value="concat('Hello ', PersonGivenName)">
       <xf:label>Output: </xf:label>
     </xf:output>
  </body>
</html>

This basic form illustrates the incremental mode of input field handling. As you type, the page updates and shows some details of xforms event processing. This is an easy way to check that everything is working properly on your system.

Troubleshooting

Some things to watch out for:

  • As shown in the example above, you may need to deliver the file with the "application/xml" content type for browsers to handle it correctly. If you find the static text of your page displaying but none of the form controls, this is likely the problem.
  • If the XML sent or received by the form make use of namespaces, you might need a workaround to work with Firefox, due to an outstanding bug in that browser. More details are available in the project Bugzilla database.

XSLTForms has an active community of supporters that answer questions on the project mailing list. Most beginners' problems have already been solved and message archives contain a wealth of information on solutions. Another thing to try is a recent build from the SVN repository, since in some cases bugs have been fixed subsequent to release.

Conclusion

When it comes to working with structured data, it's hard to beat XForms. New engines like XSLTForms make it easy to include XForms in your application development cycle. Why not give it a try today?

Comments

  • This article is a little dated now. Is this the same as XForms? Has XForms been integrated into MarkLogic? What I'm looking for is a tool that will automatically generate web-based forms based on an XML schema or XML data file. 
    •  Rich, did you ever get an answer to this question?
      • This brief tutorial is about xsltforms, an implementation of XForms.  MarkLogic does not provide an XForms implementation.
        •  Been looking at this for a while, this example works with XLSTForms, the lastest Beta-3 from the Source, NOT the main download of the site. Also I found that it is included in a folder in Marklogic. The workaround for Firefox worked perfectly for me. Just added the following: declare namespace nfunc = "{namespaceMyXmlFileUsing}"; ... xdmp:set-response-content-type("application/xml"), <?xml-stylesheet href="xsltforms.xsl" type="text/xsl"?>, <html  xmlns="http://www.w3.org/1999/xhtml"  xmlns:xf="http://www.w3.org/2002/xforms"  xmlns:{myXML's namespace}=...  xmlns:ev="http://www.w3.org/2001/xml-events"  nfunc:bogus="Workaround for FF's bug: https://bugzilla.mozilla.org/show_bug.cgi?id=94270"  >