Xforms Tutorial

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

XFORMS TUTORIAL

Introduction To XForms
XForms is the next generation of HTML forms.
XForms uses XML to create input forms on the Web.

What You Should Already Know


Before you continue you should have a basic understanding of the following:
• HTML
• HTML Forms
• XHTML
• XML

What Is XForms?
XForms is the next generation of HTML forms
XForms is richer and more flexible than HTML forms
XForms will be the forms standard in XHTML 2.0
XForms is platform and device independent
XForms separates data and logic from presentation
XForms uses XML to define form data
XForms stores and transports data in XML documents
XForms contains features like calculations and validations of forms
XForms reduces or eliminates the need for scripting
XForms is a W3C Recommendation
XForms Is The Successors Of HTML Forms

Forms are an important part of many web applications today. An HTML form makes it possible for web
applications to accept input from a user.

Today, ten years after HTML forms became a part of the HTML standard, web users do complex
transactions that are starting to exceed the limitations of standard HTML forms.

XForms provides a richer, more secure, and device independent way of handling web input. We should
expect future web solutions to demand the use of XForms-enabled browsers (All future browsers should
support XForms).
XForms Separate Data From Presentation
XForms uses XML for data definition and HTML or XHTML for data display. XForms separates the data logic
of a form from its presentation. This way the XForms data can be defined independent of how the end-user
will interact with the application.
XForms Uses XML To Define Form Data
With XForms, the rules for describing and validating data are expressed in XML.
XForms Uses XML To Store And Transport Data
With XForms, the data displayed in a form are stored in an XML document, and the data submitted from the
form, are transported over the internet using XML.
The data content is coded in, and transported as Unicode bytes.

XForms Is Device Independent


Separating data from presentation makes XForms device independent, because the data model can be
used for all devices. The presentation can be customized for different user interfaces, like mobile phones,
handheld devices, and Braille readers for the blind.
Since XForms is device independent and based on XML, it is also possible to add XForms elements directly
into other XML applications like VoiceXML (speaking web data), WML (Wireless Markup Language), and
SVG (Scalable Vector Graphics).

XForms Is A W3C Recommendation


XForms 1.0 became a W3C Recommendation in October 2003.

The official W3C recommendation can be found at this link:


http://www.w3.org/TR/xforms/

1
XForms Model
The XForms model defines a template for the data to be collected from a form.

The XForms Framework


The purpose of an HTML form is to collect data. XForms has the same purpose.
With XForms, input data is described in two different parts:
The XForm model - defines what the form is, what it should do, what data it contains
The XForm user interface - defines the input fields and how they should be displayed
The XForms Model

The XForms model describes the data.

The XForms model defines a data model inside a model element: <model>
<instance>
<person>
<fname/>
<lname/>
</person>
</instance>
<submission id="form1" action="submit.asp" method="get"/>
</model>

In the example above, the XForms model uses an instance element to define the XML-template for the data
to be collected, and a submission element to describe how to submit the data.

Note: The XForms model does not say anything about the visual part of the form (the user interface).
XForms Namespace

If you are missing the XForms namespace in these examples, or if you don't know what a namespace is, it
will be introduced in the next chapter.
The instance Element

The instance element defines the data to be collected.

XForms is always collecting data for an XML document. The instance element in the XForms model defines
the XML document.

In the example above the "data instance" (the XML document) the form is collecting data for looks like this:
<person>
<fname/>
<lname/>
</person>

After collecting the data, the XML document might look like this:
<person>
<fname>John</fname>
<lname>Smith</lname>
</person>

The submission Element


The submission element describes how to submit the data.
The submission element defines a form and how it should be submitted.
In the example above, the id="form1" identifies a form, the action="submit.asp" defines the URL to where
the form should be submitted, and the method="get" attribute defines the method to use when submitting the
form data.

The XForms User Interface


The XForms user interface defines the input fields and how they should be displayed.

The user interface elements are called controls (or input controls):
<input ref="fname"><label>First Name</label></input>

2
<input ref="lname"><label>Last Name</label></input>
<submit submission="form1"><label>Submit</label></submit>

In the example above the two <input> elements define two input fields. The ref="fname" and ref="lname"
attributes point to the <fname> and <lname> elements in the XForms model.

The <submit> element has a submission="form1" attribute which refers to the <submission> element in the
XForms model. A submit element is usually displayed as a button.

Notice the <label> elements in the example. With XForms every input control element has a required
<label> element.

You'll Need A Container


XForms is not designed to work alone. There is no such thing as an XForms document.
XForms has to run inside another XML document. It could run inside XHTML 1.0, and it will run inside
XHTML 2.0.

All Together Now


If we put it all together, the document will look like this:
<xforms>
<model>
<instance>
<person>
<fname/>
<lname/>
</person>
</instance>
<submission id="form1" action="submit.asp" method="get"/>
</model>
<input ref="fname"><label>First Name</label></input>
<input ref="lname"><label>Last Name</label></input>
<submit submission="form1"><label>Submit</label></submit>
</xforms>

The XForms Processor


An XForms Processor built into the browser will be responsible for submitting the XForms data to a target.
The data can be submitted as XML and could look something like this:
<person>
<fname>Hege</fname>
<lname>Refsnes</lname>
</person>

Or it can be submitted as text, looking something like this:fname=Hege;lname=Refsnes

XForms Namespace
You should use an XForms namespace in HTML and XHTML 1.0.
But hopefully not in XHTML 2.0.

The XForms Namespace


The official namespace for XForms is:
http://www.w3.org/2002/xforms
If you want to use XForms in HTML (or XHTML 1.0), you should declare all XForms elements with an
XForms namespace.
XForms is expected to be a standard part of XHTML 2.0, eliminating the need for the XForms namespace.

This example uses the XForms namespace:


<html xmlns:xf="http://www.w3.org/2002/xforms">
<head>
<xf:model>

3
<xf:instance>
<person>
<fname/>
<lname/>
</person>
</xf:instance>
<xf:submission id="form1" method="get" action="submit.asp"/>
</xf:model>
</head>
<body>
<xf:input ref="fname"><xf:label>First Name</xf:label></xf:input><br />
<xf:input ref="lname"><xf:label>Last Name</xf:label></xf:input><br /><br />
<xf:submit submission="form1"><xf:label>Submit</xf:label></xf:submit>
</body>
</html>

In the example above we have used the xf: prefix for the XForms namespace, but you are free to call the
prefix anything you want.

XForms Example
You can test XForms with Internet Explorer (XForms will not work in IE prior version 5).
<model>
<instance>
<person>
<fname/>
<lname/>
</person>
</instance>
<submission id="form1" method="get"
action="submit.asp"/>
</model>
<input ref="fname">
<label>First Name</label></input><br />
<input ref="lname">
<label>Last Name</label></input><br /><br />
<submit submission="form1">
<label>Submit</label></submit>
</xforms>

You might also like