Comments? Feedback?

This wiki does not yet support public comments (a limitation of Google Sites), so we encourage you to post your comments either:

On Twitter by responding to @orbeon.

On our community mailing list: subscribe sending an email to ops-users-subscribe@ow2.org (content of subject/body doesn't matter), you'll get a response with the email to use to send your message to the community mailing list.

Recent site activity

How-to guides‎ > ‎XForms Logic‎ > ‎

Load initial form data

The problem

You just created a nice form with XForms. But how do you get it to load initial data, for example coming from a database or a service? You have two ways of doing this, depending on whether a push or pull method is more suitable to your situation.

If you designed your form with Form Builder, rather than writing XForms by hand, see how to load an initial instance in Form Runner.

The "push" solution

Many frameworks use a solution that goes like this:
  • The "model" layer or backend fetches data from services and databases.
  • It stores them in objects.
  • It then calls up the "view" layer, which somehow accesses those objects.
In other words, your application pushes its data to the view, implemented in XForms.

In the Java world, frameworks like Apache Struts implement this type of solution.

With Orbeon Forms, you can achieve something similar when using Java and the Orbeon XForms filter:
  • Store data in the request in the form of a DOM or a String containing serialized XML.
  • Extract the data upon form initialization with the xxforms:get-request-attribute() function.
Suppose the following initially empty instance:

<xforms:instance id="user-data">
    <registration>
        <first-name/>
        <last-name/>
    </registration>
</xforms:instance>

Your Java code stores the property into the request, e.g.:

request.setAttribute("my-user-data", myDataAsXML)

In the XForms page, the following action reads the attribute and uses it to replace the initial content of the user-data instance:

<xforms:insert ev:event="xforms-model-construct-done"
               nodeset="instance('user-data')"
               origin="xxforms:get-request-attribute('my-user-data')"/>

Run it and get the source

The "pull" solution

With XForms, another solution often feels more natural, maybe because XForms has a natural fit for web services. It goes like this:
  • The XForms page is loaded (not necessarily involving any Java code at all).
  • Upon initialization, the page calls a service (with <xforms:submission>) which fetches the initial data
In other words, the XForms page pulls the data from a service.

You do this simply with a submission that runs during form initialization:

<xforms:send ev:event="xforms-model-construct-done" submission="load-data-submission"/>
<xforms:submission id="load-data-submission"
                   method="get" serialization="none"
                   resource="http://example.org/service/load-initial-form-data-pull-instance"
                   replace="instance" instance="user-data"/>

Often, initial instance data depends on parameters. A submission running during initialization can get and set URL parameters:

<xforms:submission id="load-data-submission"
                   method="get" serialization="none"
                   resource="http://example.org/service/load-initial-form-data-pull-instance?customerId={xxforms:get-request-parameter('customerId')}"
                   replace="instance" instance="user-data"/>

The above submission passes through to the called service a parameter called customerId:
  • It reads it from the current request parameter using the xxforms:get-request-parameter() function.
  • It appends it to the submission URL so that the service can return data specific to the given customer.
NOTE: In the submission, you are not limited to using the HTTP GET method and request parameters: you can configure the submission to obtain data through POST, for example.

If all the submission is doing is using HTTP GET and if the URL of the service is static, you can get by with a simple src or resource attribute on <xforms:instance>:

<xforms:instance id="user-data" src="http://example.org/service/load-initial-form-data-pull-instance"/>

The simple <xforms:instance> solution works in scenarios where the data to retrieve is static, or only dependent on the time it is retrieved, or some parameters to the current request.

NOTE: In that case, you cannot append dynamic request parameters as with <xforms:submission>.

Run it and get the source

The "POST" solution

If your XForms page responds to an HTTP POST containing XML, then it can access the content of the POST data with a special URL called input:instance:

<xforms:instance id="user-data" src="input:instance"/>

This results in the user-data instance being populated with the XML data posted to the XForms page. It's as easy as this!

NOTE: Nothing prevents you to combine this method with getting data from the request or a service.

The "dynamically generated page" solution

In theory, you could also, when using Java and/or JSP, directly insert the right instance data into a dynamically-generated form.

This however has some negative performance impact, because this means that the XForms engine can no longer cache the page definition, because it is likely to change at every request.

Therefore, we do not recommend this solution, and instead using a request object or a service as described above.