Generating a form from an XML schema

A returning question on the cocoon user list is "Can I generate a Cocoon Form from an XML schema?". At Hippo (http://www.hippo.nl), we have built several websites that make use of this concept. I have put together a small example that demonstrates transforming an XML schema into a Cocoon Forms definition, template and binding using three similar XSLs.

The XSLs in this example were actually one of my firsts attempts at XSL/Schema/Cocoon, so they are not examples of very good programming. In addition, they can only transform schemas of a certain, very simple and restricted structure. These XSLs should be considered as a proof of concept, rather than an example of how it should be done.

The difficulty with schemas is that they can contain an implicit structure by means of references to elements. The example XSLs can transform schemas with the following structure: a root element containing references to a number other elements, each having a number of child elements. See the included schema for an example. Try to edit the schema and see the form adapt to it. Making forms has never been easier (wink)

Installation notes

  1. Install Cocoon 2.1.9 2. Create a folder schema2form under your cocoon/webapp folder. 3. Download schema2form_cocoon_2_1_9.zip and unzip it inside schema2form. 4. Start Cocoon 5. Point your browser to http://localhost:8888/schema2form/form-example

The ZIP file contains 8 files:

  • sitemap.xmap
  • flow.js: flowscript for the form
  • lib-io.js: some IO functions
  • schema2formmodel.xsl: XSL transforming a schema intro a Cocoon Forms definition
  • schema2formtemplate.xsl: XSL transforming a schema intro a Cocoon Forms template
  • schema2formbinding.xsl: XSL transforming a schema intro a Cocoon Forms binding definition
  • example.xsd: an example XML schema that can be transformed into a form
  • example.xml: empty XML file used to bind the form to

Remarks

  • The latest version (schema2form_cocoon_2_1_9.zip) now works with Cocoon 2.1.9 and is easier to install then the previous versions.
  • The v2 version of the ZIP file contains updated XSLs with limited support for repeaters. Any element with a maxOccurs="unbounded" attribute will result in a repeater in the form. This code was incomplete and commented out in the previous version (schema2form.zip).
  • I noticed that with large schemas/forms, checking checkboxes or radiobuttons becomes a very frustrating activity, as the time between clicking and seeing the check appear can be more than a second. See my post on the user list at http://marc.theaimsgroup.com/?l=xml-cocoon-users&m=108323069201433&w=2 for a description of the problem.

Feedback wanted

If you have difficulties on getting the example up and running, or if you have any comments, please send me an email and I will try to help. Also feel free to add useful info to this Wiki page.

NielsVanKampenhout

  • No labels