XForms Model

The XForms model defines a template for focusing on the data in the form.

XForms framework

The role of HTML forms is to collect data. The role of XForms is the same.

Through XForms, input data is described in two different parts:

  • XForms model (describing data and logic)
  • XForms user interface (displaying and entering data)

XForms ModelDefines what a form is, what data it contains, and what it should do.

XForms user interfaceDefines input fields and how they are displayed.

XForms Model

XForms ModelForDescriptionData.

A data model is an instance of an XML document (a template).

The XForms model defines a data model within a <model> element:

<model>
<instance>
  <person>
    <fname/>
    <lname/>
  </person>
</instance>
<submission id="form1" action="submit.asp" method="get"/>
</model>

From the above example, you can see that the XForms model uses a <instance> elements to define an XML template for the data to be collected, and use a <submission> elements to describe how to submit data.

<submission> modelThere is noExpress any information about the visible part of the form (user interface).

XForms Namespace

If you have ignored the XForms namespace in these examples, or if you are not clear about the concept of namespace, we will introduce this content in the next section.

<instance> element

XForms Model <instance> elements to define the data to be collected.

XForms isTo collect data for an XML documentIn this XForms model, the <instance> element defines this XML document.

In the above example, the form collects data for its data instance (XML document) similar to this:

<person>
  <fname/>
  <lname/>
</person>

After the data collection is complete, this XML document may look like this:

<person>
  <fname>John</fname>
  <lname>Smith</lname>
</person>

The <submission> element

The XForms model uses <submission> elements to describe how to submit data.

The <submission> element can define the form and how to submit the form. In the above example,id="form1" is used to identify this form,action="submit.asp" The attribute specifies the URL to which the form is submitted, while method="get" The attribute defines the method used during the form submission process.

XForms user interface

XForms User interfaceForDisplay and inputData.

The user interface elements of XForms are calledControlOr input control:

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

In the above example, 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 submission="form1" attribute in the <submit> element can refer to the <submission> element in the XForms model. The submit element is usually displayed as a button.

Please note the <label> element in the example. With XForms, every input control element must have a <label> element.

You will need a container

XForms was not designed to work independently. There is no such thing as an XForms document.

XForms must work in another XML document. It can run in XHTML 1.0, and it will also work in XHTML 2.0.

Now let's put all of them together.

If we combine them, 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 page will be displayed as follows:

xforms_example

XForms Processor

Built-in to the browser XForms Processorwill be responsible for submitting XForms data to the target.

Data can be submitted as XML, like this:

<person>
  <fname>David</fname>
  <lname>Smith</lname>
</person>

Or submit it as text, like this:

fname=David;lname=Smith