XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.


Web-based XML Editing with W3C XML Schema and XSLT

April 30, 2003

W3C XML Schema (WXS) is an XML language for describing and constraining the content of XML documents. Using WXS to validate XML instance documents has become a common practice. Using a schema we can also go the other way around: create or edit a valid XML instance document using the information in the schema.

This article describes a technique in which an XML instance document can be edited through an automatically created form-based GUI, based on the schema of the instance document. The whole cycle of GUI creation (using XSLT), editing, and updating (using XUpdate) XML instances is presented here.

General Idea

The following figure illustrates the general idea: an XML instance document that can be edited through a GUI, which is created from the corresponding schema.

General idea
Figure 1. Desired system outline.

Transforming Schemas Into GUIs

The first step, one might think, is the direct transformation of the schema through a processor (which can be an XSL transformation) to a GUI.

Direct transformation of XML Schema into a form-based GUI
Figure 2. Direction transformation of a schema into a form-based GUI

The schema (labeled as XSD) is transformed through a processor into a GUI. The processor makes an input field for every (simple) element that it encounters in the XSD document. A possible approach for this step was introduced in Transforming XML Schemas in which the XSD could be transformed into a form-based GUI using XSLT.

In this article the whole concept is actually worked out for a web-based GUI, hence a servlet could be used to receive and process the input from the user. After the schema is processed the user can fill in the data into the input fields and the data is sent to the servlet. The servlet will then use this incoming data to make an XML document, which would be valid against the original schema.

The limitation of this approach is that it works only for the creation of a new XML instance document, as the processor can only transform the schema into a GUI. An existing XML instance cannot be edited, as the processor has no knowledge of transforming an XML instance document into a form-based GUI.

Editing an XML Instance Document

What we want is a processor which can take an existing XML instance document and create a form-based GUI so that we can edit the document. See the following figure.

Editing an XML instance document
>Figure 3. Editing of an XML instance document.

The concept shown in Figure 3 is as follows: instead of transforming the schema directly to a GUI, the processor will now make an XSLT stylesheet, which we call XSLGUI. This stylesheet knows exactly how to transform an XML instance document valid against our schema into a form-based GUI.

To give an example of how the concept works, imagine you have a schema document called Person.xsd in which you define what elements a person can have (e.g name, last_name, ...):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="1.0" 

  <xsd:element name="person">
        <xsd:element name="name" type="xsd:string"/>
        <xsd:element name="phone" type="xsd:string" 
            minOccurs="1" maxOccurs="3"/>
        <xsd:element name="date_of_birth" 
        <xsd:element name="course" minOccurs="0" 
              <xsd:element name="course_name" 
              <xsd:element name="start_date" 

Imagine that you also have created an instance document for an imaginary person called Marc (Marc.xml) using the Person.xsd:

<?xml version="1.0" encoding="UTF-8"?>

      <course_name>Java Programming</course_name>

The cycle shown in Figure 3 will use the Person.xsd document to make an XSLT (XSLGUI) document, which is able to make a GUI based on the elements found in Marc.xml.

There are different approaches possible for the way XSLGUI can create a form-based GUI based on the instance document. The first approach would be using XForms, which is currently a W3C Candidate Recommendation. The XSLGUI should then produce the right XForms XML document based on the XML instance document (e.g Marc.xml). Using XForms, however, means that the browser should be XForms-enabled, and as XForms is not a recommendation yet, the commonly used browsers do not support it.

A second approach would be Apache Cocoon XMLForm. The main difference between Cocoon XMLForm and W3C XForms is that XMLForm can be used with any browser for any client device as it does not require that the clients understand the XForms markup (See XML Forms, Web Services and Apache Cocoon).

A possible third approach, presented in this article, is a combination of XPath, XUpdate, and W3C XML Schema validation. The reason we have chosen this approach is because it is not dependent on the browser (browsers do not support XForms yet) or on using Cocoon (if you choose XMLForms you have to use Cocoon).

Pages: 1, 2

Next Pagearrow