XML 2003 logo

W3C XForms

Quick Reference to the Recommendation

Abstract

The XForms specification [XForms 1.0]represents a way of looking beyond Web forms, to business applications and interactive Web Services, where a human interface meshes with machine-processed XML. This paper provides a quick reference to the major components of the W3C XForms Recommendation.

Keywords


Table of Contents

1. What's wrong with classic HTML Forms?
1.1. HTML forms are untyped and lack validation facilities
1.2. HTML forms offer a limited "flat" data model
1.3. HTML forms are difficult to apply to workflow and routing
1.4. HTML forms have limited accessibility
1.5. HTML forms make internationalization more difficult
1.6. HTML forms don't work well on non-desktop-browser devices
1.7. HTML forms are tied to JavaScript
2. What are XForms?
2.1. What does XForms 1.0 get right?
2.1.1. XForms has a type system and include rich validation facilities
2.1.2. XForms supports XML processing
2.1.3. XForms is ideal for workflow and routing
2.1.4. XForms enables great accessibility
2.1.5. XForms makes internationalization easier
2.1.6. XForms is designed for different classes of devices
2.1.7. XForms is largely script-free
2.2. Connecting the pieces
2.2.1. Form controls and user interface
2.2.2. Constraints
2.2.3. XForms Actions
2.2.4. Methods
2.2.5. Submitting data
3. What's next?
3.1. Even more implemenations
3.2. Conclusions
Acknowledgements
Bibliography
Biography

1. What's wrong with classic HTML Forms?

It's hard to believe that HTML forms have been with us for over a decade. This technology has been the workhorse of web shopping, auctions, searches, polls, and interactive design—but it has limitations.

1.1. HTML forms are untyped and lack validation facilities

All data in HTML forms are essentially treated like strings. Common validations such as dates, times, email addresses, postal codes, etc. are not expressible as markup.

1.2. HTML forms offer a limited "flat" data model

Data submitted from HTML forms consist solely of flat name/value pairs. More richly structured data, such as XML, isn't possible. Working with XML data sources, as input or output, requires additional layers of software on the server.

1.3. HTML forms are difficult to apply to workflow and routing

Every form control has different operational parameters for providing initial data. A few examples:

Form control Processing for initial data
<input type="text" value="***"> Insert value attribute
<textarea>***</textarea> Insert text content
<select><option selected>... Insert selected attribute at proper location (or locations)

Table 1. 

In each case, the initial data must be specified inline with the rest of the form. In workflow/routing scenarios, where the results of one form are directed to another form, this design requires either dynamic document assembly or sophisticated search/replace functions on the server. This kind of IO-intensive processing puts a bottleneck on even moderate volume form processing.

1.4. HTML forms have limited accessibility

Even fundamental concepts, such as ensuring a title is structurally connected to a form control, aren't mandated in HTML forms. As a result, a generation of developers has become dependent on tables and other tricks that obscure the structure of the form at the markup level. This makes life more difficult for alternate rendering technologies, including voice browsers.

1.5. HTML forms make internationalization more difficult

The common application/x-www-form-urlencoded format for submitted data is poorly defined for characters outside the US ASCII range. As a result, form submission is often encoded with incompatible or proprietary schemes.

1.6. HTML forms don't work well on non-desktop-browser devices

HTML form controls are limited and even inappropriate in some contexts found on small devices. For example, an <input type="radio"> form control is tied to a specific representation, which might not make sense on a small device.

1.7. HTML forms are tied to JavaScript

The primary way to add application-specific processing to HTML forms is through scripts (in practice, JavaScript). As a result, forms are more expensive to develop, troubleshoot, and maintain.

2. What are XForms?

"XForms 1.0" is a W3C Recommendation that addresses many shortcomings of classic HTML forms ([XForms 1.0]). XForms is not a freestanding document type, but rather provides a description of markup and processing for integration into other markup languages, such as XHTML 2.0 and SVG.

XForms markup always lives inside another document, called the containing document. Within the containing document, in separate locations, are the parts that describe the form operation and presentation.

The core form definition is called the XForms Model, which contains or links to the initial XML form data, called instance data.

The user interface components are called form controls, which attach to the XForms Model through bindings. The overall application that processes XForms is called an XForms Processor.

2.1. What does XForms 1.0 get right?

2.1.1. XForms has a type system and include rich validation facilities

XForms can make use of XML Schema datatype information, when available, as well as support rich validations expressed with declarative XPath.

2.1.2. XForms supports XML processing

Data submitted from XForms can be formatted as XML that conforms to any DTD or Schema, or simply well-formed. Legacy submission formats from classic HTML forms are also supported.

2.1.3. XForms is ideal for workflow and routing

Initial form data can be specified from a separate XML document. Every form control has an identical interface to the form instance data. Thus, the results of one form can be easily directed into another form.

2.1.4. XForms enables great accessibility

Designed from the ground up for accessibility, XForms supports and encourages form designers to follow best practices to achieve accessible design.

2.1.5. XForms makes internationalization easier

The combination of XML and Unicode enables consistent treatment of ASCII-excluded characters in form data or user interface. XForms have built-in conversion support for different representations of decimal numbers, dates, and times.

2.1.6. XForms is designed for different classes of devices

Different conformance profiles, called “XForms Full” and “XForms Basic” enable the deployment of forms across devices from powerful workstation computers to tiny phones and appliances.

2.1.7. XForms is largely script-free

Instead of script, declarative markup provides needed functionality. Scripting can still be used if needed, but often won’t be.

2.2. Connecting the pieces

Historically, classic HTML forms have been authored with JavaScript on the client side, and a completely separate server application to accept and double-check the data. The advent of XForms, however, permits a single XForms Model to provide both client-side processing as well as server-side.

The XForms Model is suitable for use with a variety of front-ends, beyond XHTML, including voice-specific markup, as well as various server architectures.

<model schema="/schemas/instance.xsd">
   <instance src="/cgi-bin/get-instance"/>
   <submission action="/cgi-bin/form-submit"/>
</model>

Form data is both obtained and send back as XML. A reference to XML Schema can provide additional metadata, at the option of the XForms engine.

Figure 1. Example of an XForms Model

Largely, the XForms 1.0 specification consists of concrete definitions of existing common practice in forms authoring.

2.2.1. Form controls and user interface

Form controls defined in XForms are similar to those defined in classic HTML forms, though better generalized and less specific to a particular hard-coded implementation choice:

XForms form control Classic HTML Forms equivalent
<input> <input type="text">
<secret> <input type="password">
<textarea> <textarea>
<output> No equivalent
<range> No equivalent
<upload> <input type="file">
<select1> <select> or <input type="radio">
<select> <select> or <input type="check">

Table 2. 

Input, secret, and textarea all have similar counterparts in HTML. The XForms form controls, however, provide localization (for example, European users can enter “1,23” for a decimal number) and support for input modes, including eastern character sets and T9 predictive input on phone handsets.

Output is new in XForms, and provides a way to incorporate current information and summaries into form design.

Range is also new, providing smooth “volume control” selection from an ordered sequence.

Upload has a similar counterpart in HTML forms, but the XForms version accomplishes much more, and includes "device" upload, enabling microphones, scanners, cameras, and pen input.

Select and select1, finally, express the concepts of picking many or one items from a list. List selection items may be inline, as in HTML, or dynamic, based on instance data.

Form controls uniformly accept certain child elements that allow authors to provide additional information and functionality:

Help

Text and markup describing a help message, available on request

Hint

Text and markup describing a hint shown as needed

Alert

Additional text and markup for an additional alert message

XForms Actions

Described in a following section.

Beyond individual form controls, XForms also specifies:

Group

to logically group form controls and other markup

Switch

to dynamically present one of many possible options

Repeat

to present repeating structures, such as "line items"

<input ref="timecard/weekly/tue" inputMode="latin digits">
   <label>Hours Worked:</label>
   <hint>Enter a whole number of hours worked</hint>
</input>

Figure 2. Typical XForms form control markup

2.2.2. Constraints

A number of various XPath constraints and computations can be applied to the form data, using specific attributes on binding elements:

Required

Is this node required to be non-empty?

Reaonly

Is this node restricted from changing?

Relevant

Should the user interface for this node be presented?

Calculate

Compute the value of this node by the given expression.

<model>
   ...
   <bind ref="timecard/weekly/*" required="1"/>
   <bind ref="timecard/total" calculate="sum(../weekly/*)"/>
   ...
</model>

Figure 3. XForms constraints attached through binding.

2.2.3. XForms Actions

In the course of form processing, often some particular action needs to happen. The XForms specification builds upon [XML Events], to trigger actions in response to various events. Individually, each XForms Action replaces only a small amount of script, but taken as a whole and in various combinations, about 80% of the most common scripting can be eliminated.

XForms Action Description Similar JavaScript
setfocus Gives focus to a particular form control Control.focus();
setvalue Sets the value of a particular node Control.value = val; or list iteration setting selected attributes
message Displays a message to the user alert( "message");
send Submits all or part of the instance data Form.submit();
reset Resets all or part of the instance data Form.reset();
load Opens a new document document.location = x;
refresh Refreshes the view of the instance data N/A
recalculate Recalculates the instance data N/A
revalidate Revalidates the instance data N/A
setindex Navigates through a repeating sequence N/A
insert Inserts a node into a repeating sequence InsertNode(...);
delete Removes a node from a repeating sequence RemoveNode(...);
toggle Dynamically renders specific content N/A
script Execute arbitrary script (from host language) N/A
dispatch Dispatch an XML Event dispatchEvent(evt);

Table 3. 

<input bind="searchParams">
   <label>Search Terms:</label>
   <send ev:event="DOMActivate" submission="sid"/>
</input>

An XForms Action, causing the form to submit upon pressing the "enter" key (or equivalent).

Figure 4. An XForms Action

2.2.4. Methods

The XForms specification uses and builds upon XPath, which includes adding some method calls useful for forms:

Method Description
avg() Returns the arithmetic mean
min() and max() Returns the minimum/maximum
count-non-empty() Counts the number of non-empty nodes
if() Returns one of two strings depending on a Boolean evalutation
boolean-from-string() Converts "true" or "false" into a Boolean value
index() Gets the current position in a repeating sequence
property() Returns various properties of the XForms Processor
now() Returns the current date/time

Table 4. 

These can be called at any point where XPath is allowed. Additionally, implementations may support common "extension functions", including many from EXSLT [EXSLT], to provide additional functionality.

<bind ref="sales/commission"
   calculate="if( sales/total &gt; 100000, sales/total * 0.08, 0 )"/>

Figure 5. Calling a method as part of a calculation

2.2.5. Submitting data

XForms Processing maintains the instance data in an internal XML structure. Upon submit, the XML is serialized into one of the following formats before sending over the wire:

application/x-www-urlencoded

Internationalized equivalent to the GET method in classic HTML forms

multipart/form-data

Similar to the POST method in classic HTML forms

multipart/related

An XML-optimized variation of multipart/form-data

application/xml

Sending XML data

Others

Custom XML-related media types can easily be used, including application/xhtml+xml

3. What's next?

In mid-October 2003, XForms 1.0 was finalized as a W3C Recommendation. A smaller part if the specification, detailing the "XForms Basic" conformance level, remained as a Candidate Recommendation, awaiting further work and participation from small device vendors.

3.1. Even more implemenations

At the pre-Recommendation stage, the XForms specification set an all-time record for the most implementations af W3C specification prior to Recommendation. A wide variety of XForms engines are underway, with several already meeting the full conformance level. The xml.com article "Ten Favorite XForms Engines" [xml.com]outlines many of them. Implementation platforms include Java, Internet Explorer plug-ins, Flash, and several server-side transformations.

3.2. Conclusions

The development of classic HTML forms led to the interactive Web. XForms has the potential to launch a second wave of XML-based Internet applications. So, I will conclude with a few predictions:

  • XForms will displace a number of incompatible proprietary XML forms formats.

  • Vendor interoperability will be much better, but still not perfect, using XForms as an exchange format.

  • XForms will find acceptance in servers first, and later in mainstream browsers.

  • Despite their shortcomings, classic HTML forms will never completely die.

  • By late 2005, mainstream public US and UK Government forms will use XForms technology.

    .

Acknowledgements

Much of this material is taken from O'Reilly XForms Essentials; used with permission.

Bibliography

[XForms Essentials] Published by O'Reilly and associates, 2003. Full text available online at http://dubinko.info/writing/xforms/.

[XForms 1.0] W3C Recommendation track document: http://www.w3.org/TR/xforms/

[XML Events] W3C Recommendation track document: http://www.w3.org/TR/xml-events/.

[EXSLT] Web site:http://www.exslt.org

[xml.com] Article "Ten Favorite XForms Engines" at: http://www.xml.com/pub/a/2003/09/10/xforms.html

Biography

Software Engineer; Chief XML Architect

Micah is a software engineer living in Phoenix, Arizona with his wife and child, and working for Cardiff Software in San Diego. He serves as an editor and author of the W3C XForms specification, and wrote the O'Reilly book on the topic [XForms Essentials].