OPS User Guide
- Getting Started
- Core Technologies Reference
- XForms
- Page Flow
- XML Pipelines (XPL)
- OPS Technologies Reference
- Processors Reference
- API Reference
- Integration
|
XForms Reference
1. Scope
Web applications use forms to collect data from users. Orbeon PresentationServer
(OPS)'s form handling capabilities are based on XForms, namely the XForms 1.0 W3C Recommendation. This section
provides an introduction to XForms concepts and explains how to use XForms in your
OPS application.
Note
This document is considered a work in progress. While it does cover some
generic features of XForms, it focuses before all on features specific to the
OPS XForms engine. For more information about XForms, please refer to the
following resources:
2. Introduction to XForms
2.1. Origin, Today, and Tomorrow
XForms 1.0 has been designed by the W3C based on experience with HTML forms. It
was promoted to the rank of W3C Recommendation in October 2003, and a second edition of the
specification has been released in October 2005. As of December 2005, mainstream
browsers (Internet Explorer, Mozilla / Firefox, Opera, Safari) do not support
XForms natively, although XForms support in Mozilla is under way and plugins are
available for Internet Explorer. However you can already leverage the benefits
of XForms today by using a hybrid client-side / server-side XForms engine like
the one provided in OPS. The OPS XForms engine transparently generates HTML
forms and performs the work that would be done by an XForms-compliant browser.
This way you can start leveraging XForms today, be ready for upcoming
XForms-compliant browsers, and work smoothly with the mainstream browsers that
are deployed in the marketplace.
For more information about the whys and therefores of server-side XForms, please
read our article, Are Server-Side XForms Engines the Future of XForms?
(pre-conference
version and updated version).
2.2. Benefits
Compared to HTML forms, XForms offers a higher level approach to forms. The
benefits are that less programming is needed (less JavaScript, and less
server-side programming), so forms are easier to create and modify. As an
illustration, let's consider some facets of XForms:
-
XML Representation of Forms. XForms clearly defines how data
entered by the end-user is collected: it is stored in an XML document
called an XForms instance, an initially empty, "skeletal" XML
instance document that defines the structure of the data you wish to
collect from the user, which is afterwards filled out with information
collected from the user. For example, credit card information collected
on a web site can be structured as follows:
<credit-card> <type/> <number/> <expiration-month/> <expiration-year/> </credit-card>
The outcome of the user filling out a form collecting this information
could be this complete XML document:
<credit-card> <type>visa</type> <number>1234567812345678</number> <expiration-month>8</expiration-month> <expiration-year>2008</expiration-year> </credit-card>
An application using this data to do some processing (e.g. checking the
validity of the credit card) receives the above XML document. There is
no need to write code to go read HTTP request parameters, or to use a
framework performing this task: XForms does it all.
-
Declarative Constraints and Validation. More often than not,
there are constraints on the data that can be entered by the end-user.
For instance, in the example we just considered, the card number must
have 16 digits and the expiration month must be a number between 1 and
12. Traditionally code must be written to check for those constraints.
And more code must be written to handle error conditions (getting back
to the page displaying the form and showing the appropriate error
messages). All this is done is very simple and declarative way with
XForms. For instance, checking that the expiration month is valid number
between 1 and 12 can be done with:
<xforms:bind nodeset="/credit-card/expiration-month" type="xs:integer" constraint=". >= 1 and 12 >= ."/>
An error message can be attached to the "month" text field and if the
end-user enters an invalid month the XForms engine will notice that the
above constraint is not met and will display the error message. You do
not have to write any code for this to happen. We will see later how you
go about doing this with XForms in more details.
-
Declarative Event Handling. User interfaces need to react to
user event such as mouse clicks and character entry. With most UI
frameworks, developers must register event handlers and implement them
in JavaScript, Java, or other traditional imperative languages. With
XForms, a set of predefined event handlers and actions are available,
which cover a set of useful cases without requiring understanding the
complex syntax and semantic of JavaScript or Java. For example, to set a
value into an XForms instance, you write:
<xforms:setvalue ref="/credit-card/expiration-month">11</xforms:setvalue>
Once you have learned the simple built-in XForms actions, you can
combine them in sequences to obtain more complex behavior.
3. Getting Started With the OPS XForms Engine
3.1. The XForms Sandbox
The easiest way to get started with simple examples is to use the OPS XForms
Sandbox. This tool allows you to upload example XForms files from your web browser
and to see the results directly. You can access the XForms sandbox:
-
Online: visit this link
to access the online public XForms Sandbox.
-
Locally: if this documentation is produced by your local installation
of OPS, visit this link.
After submitting an XHTML + XForms file, the result, or errors, should display.
If you have changed your local XForms file, reloads that page in your browser
and this will upload again your local XForms file and the XForms Sandbox will
run the new version. To select another file to upload use your browser quotes
"back" button to return to the main XForms sandbox page.
4. Programming With XForms 1.0
4.1. XForms Model

4.1.1. Introduction
To help in our exploration of XForms we consider a specific example: an
XForms Credit Card Verifier. This example displays a simple form asking
for a credit card number and related information to be entered, as shown on
the screenshot to the right. The information entered by the end-user is
validated by a set of rules and errors are flagged in red.
First, the information contained in the form is stored in an XML document
called an XForms instance, which is the skeleton or shell that will
contain the data captured by the form. You define an XForms instance within
an xforms:instance . In the Credit Card Verifier the unique
XForms instance is declared with:
<xforms:instance id="credit-card-instance"> <credit-card> <type/> <number/> <expiration-month/> <expiration-year/> <verification-code/> <valid/> </credit-card> </xforms:instance>
The XForms instance does not have to be empty of data: it can contain
initial values for the form. Here we set the valid element to
the value "false" by default:
<xforms:instance id="credit-card-instance"> <credit-card> <type/> <number/> <expiration-month/> <expiration-year/> <verification-code/> <valid>false</valid> </credit-card> </xforms:instance>
XForms instances are always contained in an XForms model, which:
-
Declares one or more XForms instance.
-
Optionally, declares a set of rules attached to the XForms instances.
-
Optionally, declares submissions.
At a minimum, the XForms instance above must be encapsulated as follows:
<xforms:model id="main-model"> <xforms:instance id="credit-card-instance"> <credit-card> <type/> <number/> <expiration-month/> <expiration-year/> <verification-code/> <valid>false</valid> </credit-card> </xforms:instance> </xforms:model>
Note that instances and models can have an optional id
attribute. If you have only one model and one instance, the id is optional,
but it becomes very convenient when more than one model or instance are
used.
4.1.2. Model Item Properties
In addition to one or more XForms instances, an XForms model can declare a
set of "rules", called "model item properties". Let's write a set of rules
for the above Credit Card Validation form. Specifically we want to:
-
Check that the credit card number is a number and valid according to particular credit card rules
-
Check that the expiration month is valid (integer between 1 and 12)
-
Check that the expiration year is valid (4 digit number)
-
Display the "verification code" line only if the card type is Visa or MasterCard
-
Check that the verification code is valid only for Visa or MasterCard
You describe each one of those rules with an <xforms:bind>
element in the XForms model. Rules apply to elements and attributes in the
XForms instance. You specify the elements and attributes each rule applies
to with an XPath expression in the mandatory nodeset attribute.
In addition to the nodeset attribute you want to have at least
one attribute specifying the essence of the rule. We go over the all the
possible attributes later in this section, but first let's see how we can
express the above rules for the Credit Card Verifier form:
-
You specify that the credit card number must be a number with:
<xforms:bind nodeset="number" type="xs:integer"/>
The value of the type attribute is a W3C XML Schema
simple type. You can see the list of simple types in the XML
Schema primer. If the end-user enters an invalid credit card
number (i.e. not a number), an error will be displayed as shows in
the screenshot on the right.
-
You can also constrain the value of an element or attribute with an
XPath expression in the constraint attribute. For
instance you specify that the expiration month must be an integer
between 1 and 12 with:
<xforms:bind nodeset="expiration-month" constraint=". castable as xs:integer and . >= 1 and 12 >= ."/>
Note that we have decided here not to bother checking the expiration
month if no credit card number was entered.
-
Similarly, you check that the expiration year is a 4 digit number with:
<xforms:bind nodeset="expiration-year" constraint=". castable as xs:integer and string-length(.) = 4"/>
-
You hide the "verification code" text field for American Express
cards with:
<xforms:bind nodeset="verification-code" relevant="../type = 'visa' or ../type = 'mastercard'"/>
The attribute we use here is relevant . By default, everything is
relevant in the XForms instance. If a "relevant" rule is specified, the
XPath expression is evaluated for each node in the nodeset, and if the
expression returns false, then the node is not considered relevant. When
a node is not relevant, the corresponding widget is not displayed (more
on this later).
-
Finally, you check that the verification code is entered for Visa
and Mastercard:
<xforms:bind nodeset="verification-code" constraint="/credit-card/type = ('visa', 'mastercard') and . castable as xs:positiveInteger"/>
Because the verification-code element has both a
relevant and a constraint attribute, we
combine them on the same xforms:bind :
<xforms:bind nodeset="verification-code" relevant="../type = 'visa' or ../type = 'mastercard'" constraint="/credit-card/type = ('visa', 'mastercard') and . castable as xs:positiveInteger"/>
XPath expressions in xforms:bind are by default relative to the
root element of the first XForms instance. This allows you to write the
first constraint above:
-
Relatively to the root element of the first XForms instance:
<xforms:bind nodeset="number" type="xs:integer"/>
-
With an absolute path in the first XForms instance:
<xforms:bind nodeset="/credit-card/number" type="xs:integer"/>
-
Referring explicitly to the "credit-card-instance" using the
instance() function:
<xforms:bind nodeset="instance('credit-card-instance')/number" type="xs:integer"/>
Now that we have seen a few examples of model item properties, let's go over all
the XForms model item properties. Model item properties can essentially be used
for 3 purposes:
Validation |
The purpose of validation is to determine if the content of an
element or attribute in the XForms instance is valid. Invalid
values can have an impact on how a form is displayed (you might
want to highlight errors and show some information to help the
end-user to correct the issue). Also, the XForms engine makes
sure that invalid data cannot be submitted. There are 3 ways to
validate the content of an element or attribute:
-
required ― You can specify in the
required attribute an XPath expression that
determines if a value is required. The XPath can be as
simple as true() , or more complex and
depend on other values entered by the end-user. By
default values are not required.
-
type ― In the type attribute
you can specify a W3C XML Schema simple type. The
type attribute complements the
required attribute, but applies separately.

In Addition, some XML schema types have special behavior:
-
constraint ― The constraint
attribute supports any XPath expression that returns a
boolean value. If false() is returned,
then the value is considered invalid, otherwise it is
considered valid.
|
Calculation |
The purpose of calculations is to dynamically compute values.
You do this with the calculate attribute:
-
calculate ― The content of the element or
attribute will be set to the result of the evaluation of
the XPath expression in the calculate
attribute. This way you can automatically compute some
values in the XForms instance based on other values,
typically entered by the end-user. By default, nodes
that contain calculated values are read-only.
|
Visibility |
In general XForms instance nodes are not read-only and are
relevant, which means that if an XForms control is bound to that
node (e.g. a text field), the control is displayed and is
editable by the end-user. You can change this by providing XPath
expressions in the readonly and
relevant attributes:
-
readonly ― If the XPath expression in
readonly evaluates to true, the control
will be displayed in non-editable mode. Typically, in an
XHTML user interface only the current value is
displayed, instead of displaying a form element, like a
text field.
-
relevant ― If the XPath expression in
relevant evaluates to false, the control
will not be displayed at all.
|
4.2. XForms Controls
4.2.1. Controls
XForms controls are similar to HTML form elements: they include text fields,
drop down lists, checkboxes, etc. These are some differences between HTML
forms elements and XForms controls:
-
The value displayed by an XForms control comes from a node of the
XForms instance. When you declare a control, you bind it to a node
of your XForms instance with an XPath expression in the
ref attribute. For instance this text field a text
field is bound to the <number> element, which a
child of <credit-card> :
<xforms:input ref="/credit-card/number"/>
-
The way a control is rendered depends on model item properties that
apply to the node the control is bound to: if it is bound to an
invalid node then an error can be displayed; if the control is bound
to a read-only node the value is displayed in read-only mode; if the
node is not relevant the control isn't be displayed at all; if the
control is bound to a non-existing node, the control is considered
non-relevant and is not displayed;
The table below lists all the available XForms controls and shows for each one
the XML you need to use in your view, as well as an example showing that
control in action.
Control |
XForms in the view |
Example |
Text field

|
<xforms:input ref="text"/>
|
XForms Controls
|
Password field

|
<xforms:secret ref="secret"/>
|
XForms Controls
|
Text area

|
<xforms:textarea ref="textarea"/>
|
XForms Controls
|
Radio buttons

|
<xforms:select1 ref="carrier" appearance="full"> <xforms:item> <xforms:label>Fedex</xforms:label> <xforms:value>fedex</xforms:value> </xforms:item> <xforms:item> <xforms:label>UPS</xforms:label> <xforms:value>ups</xforms:value> </xforms:item> </xforms:select1>
|
XForms Controls
|
Single-selection lists

|
<xforms:select1 ref="carrier" appearance="compact"> <xforms:item> <xforms:label>Fedex</xforms:label> <xforms:value>fedex</xforms:value> </xforms:item> <xforms:item> <xforms:label>UPS</xforms:label> <xforms:value>ups</xforms:value> </xforms:item> </xforms:select1>
|
XForms Controls
|
Combo box

|
<xforms:select1 ref="payment" appearance="minimal"> <xforms:item> <xforms:label>Cash</xforms:label> <xforms:value>cash</xforms:value> </xforms:item> <xforms:item> <xforms:label>Credit</xforms:label> <xforms:value>credit</xforms:value> </xforms:item> </xforms:select1>
|
XForms Controls
|
Checkboxes

|
<xforms:select ref="wrapping" appearance="full"> <xforms:choices> <xforms:item> <xforms:label>Hard-box</xforms:label> <xforms:value>box</xforms:value> </xforms:item> <xforms:item> <xforms:label>Gift</xforms:label> <xforms:value>gift</xforms:value> </xforms:item> </xforms:choices> </xforms:select>
|
XForms Controls
|
List

|
<xforms:select ref="taste" appearance="compact"> <xforms:item> <xforms:label>Vanilla</xforms:label> <xforms:value>vanilla</xforms:value> </xforms:item> <xforms:item> <xforms:label>Strawberry</xforms:label> <xforms:value>strawberry</xforms:value> </xforms:item> </xforms:select>
|
XForms Controls
|
Trigger button

|
<xforms:trigger> <xforms:label>Add carrier</xforms:label> </xforms:trigger>
|
XForms Controls
|
Submit button

|
<xforms:submit submission="main-submission"> <xforms:label>Submit</xforms:label> </xforms:submit>
|
-
|
Submit link

|
<xforms:submit submission="main-submission" appearance="xxforms:link"> <xforms:label>Submit</xforms:label> </xforms:submit>
|
-
|
Submit image

|
<xforms:submit submission="main-submission" appearance="xxforms:image"> <xxforms:img src="images/submit.gif"/> <xforms:label>Submit</xforms:label> </xforms:submit>
|
-
|
Upload

|
<xforms:upload ref="files/file[1]"> <xforms:filename ref="@filename"/> <xforms:mediatype ref="@mediatype"/> <xxforms:size ref="@size"/> </xforms:upload>
|
Upload Control
|
Range

|
<xforms:range ref="range/value"> <xforms:send submission="countries-submission" ev:event="xforms-value-changed"/> </xforms:range>
|
XForms Controls
|
In the examples above, the labels and values for the select and
select1 controls are declared in the control element with
multiple <xforms:item> elements. Alternatively the
label/value pairs can be pulled out from the instance. You do this with an
<xforms:itemset> element (instead of
<xforms:item> elements):
<xforms:select1 ref="country" appearance="compact"> <xforms:itemset nodeset="instance('countries')/country"> <xforms:label ref="name"/> <xforms:value ref="us-code"/> </xforms:itemset> </xforms:select1>
4.2.2. Label, Alert, Help, and Hint
Nested inside each XForms control element, you can specify additional
elements that can alter the way the control is displayed. The table below
lists those elements:
Label |

|
By default a label is used in submit controls, as well as the
single and multiple selection controls, as shown in the table
above. The label element is mandatory for all
controls.
|
Alert |

|
In each control you can specify an error message that can be
displayed if the value entered by the user triggers a validation
error.
<xforms:secret ref="secret"> <xforms:alert>Invalid password</xforms:alert> </xforms:secret>
|
Hint |

|
You can specify a hint on each control, which is displayed next
to the control and becomes highlighted when the control is
selected.
<xforms:textarea ref="textarea"> <xforms:hint>Enter at least 11 characters</xforms:hint> </xforms:textarea>
|
Help |

|
If you specify a help message for a control, an icon with a
question mark is displayed next to the control. A pop-up shows
the help message when you position the mouse cursor over the
icon.
<xforms:input ref="date" class="xforms-date"> <xforms:label class="fixed-width">Birth date:</xforms:label> <xforms:help>This is supposed to be a help message explaining what a birth date is. But since you already know, it mostly serves the purpose
of showing how help messages can be attached to controls, and that they can be pretty long as they can be displayed on multiple
lines. </xforms:help> </xforms:input>
|
In the examples above, the text displayed is directly in the
<xforms:label> , <xforms:alert> ,
<xforms:help> , or <xforms:hint> element.
Alternatively that text can come from an XForms instance with a
ref attribute on any one of those elements. The
ref references a node in the instant that contains the text to
use. This is illustrated in the code below from the XForms Text Controls example:
<xforms:secret ref="secret"> <xforms:alert ref="@alert"/> </xforms:secret>
4.2.3. Upload
XForms allows you to upload files with the XForms Upload control:
<xforms:upload ref="files/file[1]"> <xforms:filename ref="@filename"/> <xforms:mediatype ref="@mediatype"/> <xxforms:size ref="@size"/> </xforms:upload>
The related section of the XForms instance can look like this:
<files> <file filename="" mediatype="" size="" xsi:type="xs:anyURI"/> </files>
The file element is the element storing the result of the file
upload. The result can be stored in two ways:
- As a URL, by specifying the type
xs:anyURI .
- As
Base64-encoded text, by specifying the type
xs:base64Binary .
Base64 is a mechanism to encode any binary data using a 65-character subset
of US-ASCII. Using this mechanism allows embedding binary data into XML
documents, at the typical cost of taking 50% more space than the original
binary data. For more information, please refer to the RFC.
Note
It is mandatory to specify either one of xs:anyURI or
xs:base64Binary .
The optional xforms:filename , xforms:mediatype , and
xxforms:size (the latter being an extension) allow storing metadata
about an uploaded file:
-
xforms:filename : stores the file name sent by the user agent
-
xforms:mediatype : store the media type sent by the user agent
-
xxforms:size : stores the actual size in bytes of the uploaded data
Note that the file name and the media type are provided by the user agent
(typically a web browser) and are not guaranteed to be correct.
The result of a file upload can look as follows when using
xs:anyURI :
<file filename="photo.jpg" mediatype="image/jpeg" size="2345" xsi:type="xs:anyURI">file:/C:/Tomcat/temp/upload_00000005.tmp</file>
Warning
The URL stored as the value of the upload is temporary and only valid for the
duration of the current request. It is only accessible from the server side, and
will not be accessible from a client such as a web browser. It is not guaranteed
to be a file: URL, only that it can be read with Presentation
Server's URL generator.
The contents of the file can be retrieved using the URL Generator. The result
will be an XML document containing a single root element containing the uploaded
file in Base64-encoded text.
Note
Using the xs:anyURI type allows OPS to make sure the uploaded
file does not have to reside entirely in memory. This is the preferred
method for uploading large files.
The result of a file upload can look as follows when using
xs:base64Binary :
<file filename="photo.jpg" mediatype="image/jpeg" size="2345" xsi:type="xs:base64Binary">/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAQDAwQDAwQEBAQFBQQFBwsHBwYGBw4KCggLEA4RERAO EA8SFBoWEhMYEw8QFh8XGBsbHR0dERYgIh8cIhocHRz/2wBDAQUFBQcGBw0HBw0cEhASHBwcHBwc
... </file>
In this case, the uploaded file is encoded an directly embedded into the XML
instance. This is a good method to handle small files only, because the entire
file is converted and stored in memory.
Make sure, in your XForms model, that you have the correct submission method and
encoding:
<xforms:submission method="post" encoding="multipart/form-data" xmlns:xforms="http://www.w3.org/2002/xforms"/>
4.3. Repeating with xforms:repeat
4.3.1. Basics
A very common requirement of user interfaces consists in repeating visual
elements, such as rows in a table or entries in a list. Those repeated
sections usually have an homogeneous aspect: they all have the same or a
very similar structure. For example, multiple table rows will differ only in
the particular content they display in their cells. An example of this is an
invoice made of lines with each a description, unit price, and quantity.
XForms provides a very powerful mechanism to implement such repeated structures:
the xforms:repeat element. You use xforms:repeat
around XHTML elements or XForms controls. For example, to repeat a table row,
you write:
<xforms:repeat> <xhtml:tr>... </xhtml:tr> </xforms:repeat>
This is not enough to be functional code: you need to indicate to the
xforms:repeat element how many repetitions must be performed. This
is done not by supplying a simple count value, but by binding the the element to
a node-set with the nodeset attribute. Consider the following
XForms instance:
<xforms:instance id="employees-instance" xmlns:xforms="http://www.w3.org/2002/xforms"> <employees> <employee> <first-name>Alice</first-name> </employee> <employee> <first-name>Bob</first-name> </employee> <employee> <first-name>Marie</first-name> </employee> </employees> </xforms:instance>
Assuming you want to produce one table row per employee, add the following
nodeset attribute:
<xforms:repeat nodeset="instance('employees-instance')/employee"> <xhtml:tr>... </xhtml:tr> </xforms:repeat>
This produces automatically three xhtml:tr rows. Note that we
explicitly use the XForms instance() function, but you may not have
to do so if that instance is already in scope. Then you display in each row the
content of the first-name element for each employee:
<xforms:repeat nodeset="instance('employees-instance')/employee"> <xhtml:tr> <xhtml:td> <xforms:output ref="first-name"/> </xhtml:td> </xhtml:tr> </xforms:repeat>
This works because for each iteration, the context node for the
ref attribute changes: during the first iteration, the context node
is the first employee element of the XForms instance; during the
second iteration, the second employee element, and so on.
Note
The nodeset attribute of xforms:repeat must point
to a so-called homogeneous collection. Such a collection must consist
of contiguous XML elements with same name and same namespace. XForms does
not predict what happens if the node-set is not homogenous.
4.3.2. Deleting Rows with the xforms:delete Action
xforms:repeat may be used purely for display purposes, but it
can also be used for interactively editing repeated data. This includes
allowing the user to delete and insert rows. Two XForms actions are used
for this purpose: xforms:delete and xforms:insert .
xforms:delete is provided with a nodeset attribute
pointing to the homogenous collection into which the insertion must take
place. It also has an at attribute, which contains an XPath
expression returning the index of the element to delete. See how
xforms:delete is used in these 3 scenarios:
<!-- This deletes the last element of the collection --> <xforms:delete nodeset="employees" at="last()"/> <!-- This deletes the first element of the collection --> <xforms:delete nodeset="employees" at="1"/> <!-- This deletes the currently selected element of the collection (assuming the repeat id 'employee-repeat') --> <xforms:delete nodeset="employees" at="index('employee-repeat')"/>
4.3.3. Inserting Rows with the xforms:insert Action
xforms:insert has a nodeset attribute
pointing to the homogenous collection into which the insertion must take
place. xforms:insert then considers the last element of
that collection (and all its content if any) as a template for the
new element to insert: it duplicates it and inserts it into the homogenous
collection at a position you specify. The last element of an homogeneous
collection therefore always acts as a template for insertions:
<!-- This inserts a copy of the template before the last element of the collection --> <xforms:insert nodeset="employees" at="last()" position="before"/> <!-- This inserts a copy of the template after the last element of the collection --> <xforms:insert nodeset="employees" at="last()" position="after"/> <!-- This inserts a copy of the template before the first element of the collection --> <xforms:insert nodeset="employees" at="1" position="before"/> <!-- This inserts a copy of the template after the first element of the collection --> <xforms:insert nodeset="employees" at="1" position="after"/> <xforms:insert nodeset="employees" at="last()" position="after"/> <!-- This inserts a copy of the template before the currently selected element of the collection --> <xforms:insert nodeset="employees" at="index('employee-repeat')" position="before"/> <!-- This inserts a copy of the template after the currently selected element of the collection --> <xforms:insert nodeset="employees" at="index('employee-repeat')" position="after"/>
The at attribute contains an XPath expression returning the
index of the element before or after which the insertion must be performed.
The position element contains either after or
before , and specifies whether the insertion is performed before
or after the element specified by the at attribute.
It is important to note that while it is possible to delete the last element
of an homogeneous collection, it becomes then impossible to insert a new
element into that collection with XForms 1.0, since there is no longer a
template element available in this case (save for using an XML submission
with replace="instance" ). This means that in general you will
want to have at least one element in your collections.
In case you want the user interface to visually appear empty empty when
there is "no more" elements in the collection, you can use the tip provided
below, which can be used in most situations. The idea is to consider that
the last element of the collection is never displayed, but always used as a
template for xforms:insert :
<xforms:instance id="employees-instance" xmlns:xforms="http://www.w3.org/2002/xforms"> <employees> <employee> <first-name>Alice</first-name> </employee> <employee> <first-name>Bob</first-name> </employee> <employee> <first-name>Marie</first-name> </employee> <!-- This is a template used by xforms:insert --> <employee> <first-name/> </employee> </employees> </xforms:instance>
You do not want to display that template, however. Therefore you use an
xforms:repeat element of the form:
<xforms:repeat nodeset="instance('employees-instance')/employee[position() < last()]">... </xforms:repeat>
The position() < last() condition tells
xforms:repeat to consider all the elements of the collection
except the last one. This causes the repetition to display zero iteration
when there is one element in the collection, one iteration when there are
two, etc. The xforms:insert action, on the other hand, operates
on the entire collection including the last element, so that that element
can be duplicated:
<xforms:insert nodeset="employees" at="..." position="..."/>
Another solution involves using an xforms:bind element which
makes the last element of the collection non-relevant. This achieves the
same result, but requires extra code, so the tip above is usually
preferred.
Upon submission, some care must be taken with repeat template. For example,
if the first-name element above is required, and the template
contains an empty value as above, submission will fail.
xforsm:bind statements must then also exclude the last element
of the collection:
<xforms:bind nodeset="employee[position() < last()]/first-name" required="true()"/>
Note
If you are dealing with an XML document format which requires
removing the last element of a collection, you have to post-process
your XForms instance to remove such extra elements, and pre-process
it to add such elements when initializing your XForms instance.
4.3.4. Using xforms:trigger to Execute Actions
Insertions and deletions are typically performed when the user of the
application presses a button, with the effect of adding a new repeated
element before or after the currently selected element, or of deleting the
currently selected element. You use an xforms:trigger control
and the XPath index() function for that purpose:
<xforms:trigger> <xforms:label>Add</xforms:label> <xforms:action ev:event="DOMActivate"> <xforms:insert nodeset="employees" at="index('employee-repeat')" position="after"/> </xforms:action> </xforms:trigger>
or:
<xforms:trigger> <xforms:label>Delete</xforms:label> <xforms:action ev:event="DOMActivate"> <xforms:delete nodeset="employees" at="index('employee-repeat')"/> </xforms:action> </xforms:trigger>
Note that we use xforms:action as a container for
xforms:insert and xforms:delete . Since there is
only one action to execute, xforms:action is not necessary, but
it may increase the legibility of the code. It is also possible to write:
<xforms:trigger> <xforms:label>Add</xforms:label> <xforms:insert ev:event="DOMActivate" nodeset="employees" at="index('employee-repeat')" position="after"/> </xforms:trigger>
or:
<xforms:trigger> <xforms:label>Delete</xforms:label> <xforms:delete ev:event="DOMActivate" nodeset="employees" at="index('employee-repeat')"/> </xforms:trigger>
Notice in that case how ev:event="DOMActivate" has been moved
from the enclosing xforms:action to the
xforms:insert and xforms:delete elements.
4.3.5. Nested Repeats
It is often desirable to nest repeat sections. Consider the following
XForms instance representing a company containing departments, each
containing a number of employees:
<xforms:instance id="departments"> <departments> <department> <name>Research and Development</name> <employees> <employee> <first-name>John</first-name> </employee> <employee> <first-name>Mary</first-name> </employee> </employees> </department> <department> <name>Support</name> <employees> <employee> <first-name>Anne</first-name> </employee> <employee> <first-name>Mark</first-name> </employee> <employee> <first-name>Sophie</first-name> </employee> </employees> </department> </departments> </xforms:instance>
This document clearly contains two nested sections subject to repetition:
-
Departments: a node-set containing all the
department elements can be referred to with the
following XPath expression:
instance('departments')/department .
-
Employees: a node-set containing all the
employee elements can be referred to with the following
XPath expression:
instance('departments')/department/employees/employee .
However, if the context node of the XPath expression points
to a particular department element, then the following
relative XPath expression refers to all the
employee elements under that department
element: employees/employee .
Following the example above, here is how departments and employees can be
represented in nested tables with XForms:
<xhtml:table> <xforms:repeat nodeset="instance('departments')/department"> <xhtml:tr> <xhtml:td> <xforms:output ref="name"/> </xhtml:td> <xhtml:td> <xhtml:table> <xforms:repeat nodeset="employees/employee"> <xhtml:tr> <xhtml:td> <xforms:output ref="first-name"/> </xhtml:td> </xhtml:tr> </xforms:repeat> </xhtml:table> </xhtml:td> </xhtml:tr> </xforms:repeat> </xhtml:table>
In the code above, the second xforms:repeat 's nodeset
expression is interpreted relatively to the department element of
the parent xforms:repeat for each iteration of the parent's
repetition. During the first iteration of the parent, the "Research and
Development" department is in scope, and employees/employee refers
to the two employees of that department, John and Mary. During the second
iteration of the parent, the "Support" department is in scope, and
employees/employee refers to the three employees of that
department, Anne, Mark and Sophie.
4.4. Actions
4.4.1. Setting Instance Values with the xforms:setvalue Action
There are two ways of providing the value to set with
<xforms:setvalue> . The first one specifies the value as a
literal enclosed in the <xforms:setvalue> element. The
second possibility uses the value attribute: the content of the
attribute is an XPath expression evaluated in the context of the node the
xforms:setvalue element is bound (through the ref
attribute). The content of the node pointed to by the ref
attribute will be set with the result of the XPath expression provided in
the value attribute. The example below and uses two
<xforms:setvalue> , each one providing the new value in a
different way.
<xforms:trigger xmlns:xforms="http://www.w3.org/2002/xforms"> <xforms:label>Submit</xforms:label> <xforms:action ev:event="DOMActivate"> <xforms:setvalue ref="clicked">my-button</xforms:setvalue> <xforms:setvalue ref="flavor" value="concat('van', 'illa')"/> </xforms:action> </xforms:trigger>
4.4.2. Displaying Message with the xforms:message Action
The XForms message action displays a message to the user. OPS being a
server-side XForms implementation.

Typically, the content of the message element is the message to
render. It can also come from the binding attributes (ref or
bind ), or from the linking attribute (src ). The order
of preference is the following:
- Binding attributes
- Linking attribute
- Inline text
Note
- The only value currently supported for the
level
attribute is modal . This attribute is optional.
- When using the linking attribute (
src ), the value must
be an absolute URL, starting with oxf: , http:
or other supported protocols.
<xforms:trigger> <xforms:label>Test</xforms:label> <xforms:message ev:event="DOMActivate" ref="taste"/> </xforms:trigger>
4.5. Submission
Two properties control some aspects of XForms submission in OPS:
<property as="xs:boolean" name="oxf.xforms.optimize-post-all" value="true"/>
If set to true (the default), OPS optimizes submissions with
replace="all" by sending the response of the submission directly to
the web browser. This however means that submission errors cannot be caught by
XForms event handlers after OPS has started connecting to the submission URL,
as should be the case following XForms 1.0. If set to false , OPS
buffers the reply so that errors can be handled as per XForms 1.0. However, this
solution is less efficient.
<property as="xs:boolean" name="oxf.xforms.optimize-local-submission" value="true"/>
-
If set to
true (the default), OPS optimizes "local" HTTP and
HTTPS submissions, i.e. submissions performed to a URL controlled by OPS
itself, by directly submitting using the Java Servlet API instead of
actually using the HTTP protocol for the submission.
-
If set to
false , OPS always always uses the HTTP or HTTPS
protocol, which is less efficient. In this case, it is possible to specify
the xxforms:post method instead of the post method
on the xforms:submission element to force an optimized local
submission.
5. Formatting
5.1. Rationale
It is usually recommended to use native XML types within XForms instances, as
this guarantees interoperability and maintainability. For example, a date of
January 10, 2005 is stored in ISO format as: 2005-10-01 . However it
is often necessary to format such values on screen in a user-readable format,
like "January 10, 2005", "10 janvier 2005", or "10. Januar 2005".
OPS provides an extension attribute, xxforms:format , for that
purpose. xxforms:format must contain an XPath 2.0 expression. In
your XPath expression you can use all the XPath 2.0 functions, including those
for date manipulation (external
documentation). However since XPath 2.0 functions don't provide any facility
for date and time formatting, you can in this attribute also use the following
XSLT 2.0 functions:
The XPath expression is evaluated by the XForms engine whenever the value bound
to the xforms:input control changes and needs to be updated on
screen. It is evaluated in the context of the instance node bound to the
control. This means that the current value of the control can be accessed with
". ". Often the value must be converted, for example to a date, in
which case the conversion can be done with XPath 2.0 type casts such as
xs:date(.) .
5.2. xforms:input
When using xforms:input and a bound xs:date type, you
can control the formatting of the date using the xxforms:format
extension attribute on the xforms:input control. For example:
<xforms:input ref="date" xxforms:format="format-date(xs:date(.), '[MNn] [D], [Y]', 'en', (), ())"/>
5.3. xforms:output
When using xforms:output , you can control the formatting of the
date using the xxforms:format extension attribute on the
xforms:input control.
<xforms:output ref="date" xxforms:format="format-date(xs:date(.), '[MNn] [D], [Y]', 'en', (), ())"/> <xforms:output ref="size" xxforms:format="format-number(., '###,##0')"/>
5.4. Default Formatting
For both xforms:input and xforms:output , if the bound
node is of type xs:date , xs:dateTime or
xs:time , and if no xxforms:format attribute is present
on the control, formatting is based on properties. If the properties are missing, a
built-in default formatting is used. The default properties, as well as the
built-in defaults, are as follows:
<property as="xs:string" name="oxf.xforms.format.date" value="if (. castable as xs:date) then format-date(xs:date(.), '[FNn] [MNn] [D], [Y]', 'en', (), ()) else ."/> <property as="xs:string" name="oxf.xforms.format.dateTime" value="if (. castable as xs:dateTime) then format-dateTime(xs:dateTime(.), '[FNn] [MNn] [D], [Y] [H01]:[m01]:[s01] UTC', 'en', (),
()) else ."/> <property as="xs:string" name="oxf.xforms.format.time" value="if (. castable as xs:time) then format-time(xs:time(.), '[H01]:[m01]:[s01] UTC', 'en', (), ()) else ."/>
They produce results as follows:
-
2004-01-07 is displayed as Wednesday January 7, 2004
-
2004-01-07T04:38:35.123 is displayed as Wednesday January 7, 2004 04:38:35 UTC
-
04:38:35.123 is displayed as 04:38:35 UTC
Note that with the condition in the XPath expressions, a value which cannot be
converted to the appropriate type is simply displayed as is.
6. XForms Instance Initialization
6.1. Rationale
An XForms page often needs to contain initial data when first loaded. The data
may come from a database, a form submitted on a previous page, etc. There are
several ways to achieve this with OPS.
6.2. Page Flow Definitions
Within your page flow, you define a page model and either a static page view:
<page id="..." path-info="..." model="my-page-model.xpl" view="my-page-view.xhtml"/>
Or a dynamic XSLT page view:
<page id="..." path-info="..." model="my-page-model.xpl" view="my-page-view.xsl"/>
The page model is in charge of producing an XML document which is then going to
be used by the page view to initialize the XForms instance. As always with OPS,
the page model produces this document on its data output, and the
page view can access this document on its data input, as shown in
the following sections. This mechanism is described in details in the PFC documentation.
6.3. Using XInclude
Following the MVC architecture, the PFC page model generates an XML document
which contains an XForms instance. A static PFC page view then includes this
instance using xi:include , as follows:
<html> <head> <title>Summary</title> <xforms:model> <xforms:instance id="document-infos-instance"> <!-- This is where the XML document produced by the page model is included --> <xi:include href="input:data"/> </xforms:instance>... </xforms:model> </head> <body>... </body> </html>
The use of the URI input:data instructs XInclude processing to
dynamically include the data output of the page view, which is
produced on the data output of the page model. Note that it is
possible to use the instance input, which then refers to the
current XML submission.
6.4. Using XSLT
It is also possible to use a dynamic XSLT page view to perform the inclusion of
the instance. XSLT is more flexible than XInclude, but less efficient at
runtime. This is an example:
<html xsl:version="2.0"> <head> <title>Summary</title> <xforms:model> <xforms:instance id="document-infos-instance"> <!-- This is where the XML document produced by the page model is included --> <xsl:copy-of select="doc('input:data')/*"/> </xforms:instance>... </xforms:model> </head> <body>... </body> </html>
Note the use of xsl:version="2.0" on the root element of the
document, which instructs the PFC to process the page view as an XSLT
stylesheet.
The use of the XPath doc() function with a URI
input:data instructs XSLT processing to dynamically include the
data output of the page view, which is produced on the
data output of the page model.
Note
It is possible to use XInclude instructions in a dynamic XSLT page view as well.
In this case, it is important to note that XInclude instructions are processed
before XSLT instructions, i.e. the result of XInclude instructions is an XSLT
stylesheet, which is then executed.
Note
Using XSLT for page views has an impact for debugging, as the output of XSLT
transformations do not contain valuable location information. For performance
and ease of debugging reasons, we recommend using static XHTML views with
XInclude whenever possible.
7. Relative Paths
7.1. Rationale
XForms documents can refer to external resources using URIs in the following
circumstances:
-
External Instances. The xforms:instance element
can have an src attribute linking to an external instance
definition.
-
Submission. The xforms:submission element must refer
to an action URI.
-
Load Action. The xforms:load action must refer to an
URI that must be loaded upon execution.
-
Image Mediatype. The xforms:output control may refer
to an image URI.
-
Message, Label, Help, Hint, and Alert.
xforms:label , xforms:help ,
xforms:hint , and xforms:alert may use an
src attribute to refer to external content.
Note
The XForms 1.1 draft of November 15, 2004 removes linking attributes
from actions and metadata elements and "the src attribute
is not available to XForms 1.1 message , label ,
help , hint , alert elements."
URIs are resolved relatively to a base URI. The base URI is, by default,
the external URL used to display the XForms page, with special handling of the
servlet context, if necessary. It is also possible to override this behavior by
adding xml:base attributes on xforms:load or any of
its ancestor elements.
7.2. External XForms Instances
Referring to external XForms instances is done with the src
attribute on the xforms:instance element:
<xforms:instance src="instance.xml"/>
This feature allows for improved modularity by separating an XForms instance
definition from an XHTML page. It also allows for producing XForms instances
dynamically.
The following assumes that OPS runs in the /ops servlet context:
Base URI (External URL or xml:base attributes)
|
Initial URI (src attribute)
|
Resolved URI |
Comment |
The following URI is loaded in a servlet:
http://a.org/ops/page
|
http://b.com/instance |
http://b.com/instance |
Absolute URLs are left untouched.
|
/new-instance |
http://a.org/ops/new-instance |
Absolute paths resolve against the current servlet context.
|
admin/instance |
http://a.org/ops/admin/instance |
The relative path resolves against the original URL.
|
The following path is loaded in a portlet:
/example/page
|
http://b.com/instance |
http://b.com/instance |
Absolute URLs are left untouched.
|
/new-instance |
/new-instance |
The absolute path is used as is. The XForms instance is loaded from the
portlet. The developer must make sure that the path resolves to a PFC
entry producing XML.
|
admin/instance |
/example/admin/instance |
The relative path is resolved against the original path. The XForms
instance is loaded from the portlet. The developer must make sure that
the path resolves to a PFC entry producing XML.
|
7.3. XForms Submisssion
Specifying a submission URL is done with the action
attribute on the xforms:submission element:
<xforms:submission action="/submission" ref="..."/>
The following assumes that OPS runs in the /ops servlet context:
Base URI (External URL or xml:base attributes)
|
Initial URI (action attribute)
|
XForms Init 1 |
Resolved URI |
Comment |
The following URI is loaded in a servlet:
http://a.org/ops/page
|
http://b.com/submission |
N/A |
http://b.com/submission |
The absolute URL is left untouched. The XForms submission is performed
on the absolute URL.
|
/new-submission |
N/A |
http://a.org/ops/new-submission |
Absolute paths resolve against the current servlet context.
|
admin/submission |
N/A |
http://a.org/ops/admin/submission |
The relative path resolves against the original URL.
|
The following path is loaded in a portlet:
/example/page
|
http://b.com/submission |
N/A |
http://b.com/submission |
The absolute URL is left untouched. The XForms submission is performed
on the absolute URL.
|
/new-submission |
Yes |
/new-submission |
The absolute path is used as is. The XForms submission is performed on
the portlet.
|
No |
http://a.org/ops/new-submission |
The absolute path resolves against the current servlet context. The
submission is performed on the web application.
|
admin/submission |
Yes |
/example/admin/submission |
The relative path is resolved against the original path. The XForms
submission is performed on the portlet.
|
No |
http://a.org/ops/ example/admin/submission |
The relative path resolves against the original path, then against the
the current servlet context. The submission is performed on the web
application.
|
1 If "yes", this means the submission is performed during XForms
initialization, for example upon an xforms-ready event. If "no",
this means that the submission is performed after XForms initialization, for
example upon the user activating a trigger.
7.4. XForms Load Action
The xforms:load action can refer to a resource to load either
through the resource attribute or using a single-node binding
retrieving the URI from an XForms instance. In both cases, the value of the URI
is resolved relatively to the base URI.
The following assumes that OPS runs in the /ops servlet context:
Base URI (External URL or xml:base attributes)
|
Initial URI (resource or Single-Node Binding)
|
show
f:url-type |
Resolved URI |
Comment |
The following URI is loaded in a servlet:
http://a.org/ops/page
|
http://b.com/software/ |
replace |
http://b.com/software/ |
The absolute URL is left untouched. The new page replaces the existing
page.
|
new |
The absolute URL is left untouched. A new window or tab opens for the new
page.
|
/new-page |
replace |
http://a.org/ops/new-page |
Absolute paths resolve against the current servlet context. The new page
replaces the existing page.
|
new |
Absolute paths resolve against the current servlet context. A new window
or tab opens for the new page.
|
admin/main-page |
replace |
http://a.org/ops/admin/main-page |
The new page replaces the existing page.
|
new |
A new window or tab opens for the new page.
|
The following path is loaded in a portlet:
/example/page
|
http://b.com/software/ |
replace |
http://b.com/software/ |
This causes the application to load a page outside of the portlet,
replacing the entire portal.
|
new |
This causes the application to load a page in a new window outside of
the portlet.
|
/new-page |
replace |
/new-page |
The resulting path is loaded within the portlet.
|
replace
f:url-type="resource" |
http://a.org/ops/new-page |
The resulting path is loaded in the same window outside the portal.
|
new |
http://a.org/ops/new-page |
The resulting path is loaded in a new window.
|
admin/main-page |
replace |
/example/admin/main-page |
The resulting path is loaded within the portlet.
|
replace
f:url-type="resource" |
undefined |
undefined |
new |
undefined |
undefined |
7.5. Image Mediatype for xforms:output
When an xforms:output control refers to an image URI, as documented
below, the resulting value is resolved
relatively to the base URI.
8. XForms and Services
8.1. Introduction
XForms 1.0 allows an XForms page to perform submissions of XForms instances and
to handle a response. In most cases, both the submitted XForms instance and the
response are XML documents.
Note
It is possible to submit an XForms instance with the HTTP GET method. In that
case, some information contained in the XML document is lost, as the structure
of the instance, attributes, and namespace prefixes among others, are not passed
to the submission.
The XForms submission feature practically allows forms to call XML services.
Those services are accessible through an XML API, which means that a request is
performed by sending an XML document to the service, and a response consists of
an XML document as well.
9. Extensions
9.1. XForms 1.1 Extensions
9.1.1. Media Type for xforms:output
In XForms 1.0, xforms:output is used to display text. However,
based on a proposal in a draft version of XForms 1.1, OPS supports a
mediatype attribute on that element.
Image Types
For the <xforms:output> control to display an image, you
need to:
-
Have a mediatype attribute on the
<xforms:output> . That attribute must refer to an
image, such as image/* or image/jpeg .
-
Use the value attribute on
<xforms:output> or bind to the control to a node
without type or with an xs:anyURI type.
The resulting value is interpreted a URI pointing to an image. The image
will display in place of the xforms:output . When a single-node
binding is used, it is possible to dynamically change the image pointed to.
For example:
<xforms:output mediatype="image/*" value="'/images/moon.jpg'"/>
<xforms:model> <xforms:instance> <image-uri/> </xforms:instance> <xforms:bind nodeset="image-uri" type="xs:anyURI"/> </xforms:model>... <xforms:output mediatype="image/*" ref="image-uri"/>
Note
It is not yet possible to directly embed image data in an XForms instance
using the xs:base64Binary type.
HTML Type
When an xforms:output control has a mediatype
attribute with value text/html , the value of the node to which
the control is bound is interpreted as HTML content. Consider the following
XForms instance:
<xforms:instance id="my-instance"> <form> <html-content>This is in <b>bold</b>! </html-content> </form> </xforms:instance>
You bind an xforms:output control to the
html-content node as follows:
<xforms:output ref="instance('my-instance')/html-content" mediatype="text/html"/>
This will display the result as HTML, as expected: "This is in in
bold!". If the mediatype is not specified, the result
would be instead: "This is in in <b>bold</b>!". In the XForms
instance, the HTML content must be escaped as text. On the other hand, the
following content will not work as expected:
<xforms:instance> <form> <html-content>This is in in<b>bold</b>! </html-content> </form> </xforms:instance>
Note
When using a mediatype="text/html" , an HTML
<div> element will be generated by the XForms engine to hold
the HTML data. As in HTML a <div> cannot
be embedded into a <p> , if you have a
<xforms:output mediatype="text/html"> control, you should
not put that control into a <xhtml:p> .
9.1.2. origin Attribute on xforms:insert Action
Based on a proposal in a draft version of XForms 1.1, OPS supports an
origin attribute on the xforms:insert action. This
attribute allows specifying the source node to use as template. This allows
storing templates separately from the node-set specified by the
nodeset attribute. For example:
<xforms:insert nodeset="address" at="last()" position="after" origin="instance('template-instance')"/>
The template copied in this case comes from an XForms instance:
<xforms:instance id="template-instance"> <address> <street> <number/> <name-1/> <name-2/> </street> <apt/> <city/> <state/> <zip/> </address> </xforms:instance>
9.2. XPath Extension Functions
OPS implements some extension functions which can be used from XPath expressions
in XForms documents.
9.2.1. XSLT 2.0 Functions
When using XPath 2.0, the following functions from XSLT 2.0 are also available:
9.2.2. OPS Functions
The following functions are implemented:
-
xxforms:call-xpl($xplURL as xs:string, $inputNames as
xs:string*, $inputElements as element()*, $outputNames as
xs:string+) .
This function lets you call an XPL pipeline.
-
The first argument,
$XPLurl , is the URL of the
pipeline. It must be an absolute URL.
-
The second argument,
$inputNames , is a sequence of
strings, each one representing the name of an input of the
pipeline that you want to connect.
-
The third argument,
$inputElements , is a sequence
of elements to be used as input for the pipeline. The
$inputNames and $inputElements
sequences must have the same length. For each element in
$inputElements , a document is created and
connected to an input of the pipeline. Elements are matched to
input name by position, for instance the element at position 3
of $inputElements is connected to the input with
the name specified at position 3 in $inputNames .
-
The fourth argument,
$outputNames , is a sequence
of output names to read.
-
The function returns a sequence of elements corresponding the
output of the pipeline. The returned sequence will have the same
length as
$outputNames and will correspond to the
pipeline output with the name specified on
$outputNames based on position.
The example below shows a call to the xxforms:call-xpl
function:
xxforms:call-xpl ('oxf:/examples/sandbox/xpath/run-xpath.xpl', ('input', 'xpath'), (instance('instance')/input, instance('instance')/xpath),
'formatted-output')/*, 'html')
9.2.3. eXForms Functions
eXForms is a suggested set of
extensions to XForms 1.0, grouped into different modules. OPS supports the
exf:mip module,
which includes the following functions:
-
exf:relevant()
-
exf:readonly()
-
exf:required()
eXForms functions live in the http://www.exforms.org/exf/1-0
namespace, usually bound to the prefix exf .
10. State Handling
10.1. Rationale
The OPS XForms engine requires keeping processing state while operating on an
XForms page. Such state includes the current values of XForms instances,
selected repeated elements, and more. With OPS, XForms state information can be
handled in one of two ways:
-
Client-side: in this case, static initial state information is
sent along with the initial HTML page, and dynamic state is exchanged
over the wire between the client browser and the OPS XForms server when
necessary.
Benefits of the approach:
-
The OPS server is entirely stateless. It only requires memory
while processing a client request. It can be restarted without
consequence for the XForms engine.
-
State information does not expire as long as the user keeps the
application page open in the web browser.
Drawbacks of the approach:
-
Resulting HTML pages are larger. In particular, the size of
state data grows when XForms instances grow, regardless of
whether many XForms controls are bound to instance data.
-
More data circulates between the client browser and the OPS
XForms server.
Note
OPS compresses and encrypts XForms state information sent to the
client.
-
Server-side: in this case, state information is stored on the
server, in association with an application session. Only very little
state information circulates between client and server.
Benefits of the approach:
-
Resulting HTML page are smaller. HTML pages increase in size as
more XForms controls are used, but they don't increase in size
proportionally to the size of XForms instances.
-
Small amounts of data circulate between the client browser and
the OPS XForms server.
-
This means that very large XForms instances can be processed
without any impact on the amount of data that is transmitted
between the client and the server.
Drawbacks of the approach:
-
The OPS XForms server needs to be stateful. It uses server
memory to store state information in a session even when no
request is being processed. The server must be configured to
determine how much state information is kept in a session, how
long session take to expire, etc. This creates additional demand
for resources on the server and complicates the task of
tuning the server.
-
State information can become unavailable when sessions expire or
when the server is restarted (unless you setup the server to
persist session information). When state information becomes
unavailable for a page, that page will no longer function unless
it is reloaded.
Note
With most servlet containers, it is possible to configure
session handling to passivate sessions out of the application
server memory to a persistent store. It is this way possible to
partially alleviate the drawback above by making sure that a
very large number of active but idle sessions can be kept, with
a minimum impact on application server memory. It is this way
also possible to make sure that sessions survive a servlet
container restart.
Note
OPS ensures that it is possible to open multiple client browser windows
showing the same page within the same session.
10.2. Configuring State Handling
State handling can be configured globally for all pages, or locally for each
individual page served. Global configuration is performed in
properties.xml with the oxf.xforms.state-handling
property. When missing or set to client , state is stored
client-side. When set to session , state is stored server-side in a
session. For example:
<!-- Store state in the session --> <property as="xs:string" name="oxf.xforms.state-handling" value="session"/>
The global configuration can be overridden for each page by setting the
xxforms:state-handling attribute in the page. This attribute can be
set for example on the root element of the XHTML page, or on the first
xforms:model element. Only the first such attribute encountered by
the XForms engine is used:
<xforms:model xxforms:state-handling="client">...</xforms:model>
When storing state in a session, the maximum size of the data to be stored for
each user can be selected using the oxf.xforms.cache.session.size
property. The size is specified in bytes:
<!-- Allow a maximum of 500 KB of state information for each user --> <property as="xs:integer" name="oxf.xforms.cache.session.size" value="500000"/>
Whether state information is kept client-side or server-side, a property
controls whether the XForms engine should try to optimize state reconstruction
by using a cache. This property should usually be set to true :
<!-- This should usually be set to "true" --> <property as="xs:boolean" name="oxf.xforms.cache.document" value="true"/>
11. JavaScript Integration
11.1. Rationale
While XForms gets you a long way towards creating a dynamic user-friendly user
interface, there are some dynamic behaviors of the user interface that cannot be
implemented with XForms, and that you might want to implement directly in
JavaScript. We describe here how your JavaScript code can interact with XForms and
in particular how you can have access to the value of an XForms control.
11.2. Accessing The Value XForms Controls
In JavaScript, you obtain a reference to the element representing the control with
var control = document.getElementById('myControl') where
myControl is the id of the XForms control (i.e. <xforms:input
id="myControl"> . You can read the current value of the control with
control.value and set the value of the control assigning a value to
control.value , for instance: control.value = 42 . When you
do such an assignment the value of the node in the instance is updated, the
xforms-value-changed event thrown, and all the standard XForms
processing happens (validation, recalculation, etc).
Note
Currently, accessing the value of a control is only supported for
<xforms:input> . If you want to access the value of another control,
a workaround consists in adding an <xforms:input> with
style="display: none" bound to the same node as that control, and
accessing the <xforms:input> 's id to change the value of the node
and therefore indirectly the other control's value.
|