The problemYou would like to store the content of an instance in the session, so you can retrieve it later, either when users come back to the same page, or from another page.
The solutionFirst, you should be aware that storing information in the session has drawbacks. Those drawbacks are not specific to XForms or Orbeon Forms; they are common to all the web applications: - Storing information in the session often breaks parallel workflows. Say you have a search page and results page. On the search page, you store the query in the session, so you can retrieve it when users come back to the search page from the results page. But what if users open two browser windows to perform two searches? Or if they perform two searches in sequence, and then use the back button to come back to the first search? In most cases the second search will overwrite information stored by the first one, leading to unexpected behavior.
- Storing information in the session makes your application less scalable:
- Because your application will use more memory, which can really become a problem if you are storing large documents, or need to support a large number of users.
- Because it will be harder to deploy your application on multiple servers, as setting up a clustered environment will pose questions of session replication, which creates more complexity and can hinder performance if you need the session to be replicated between multiple servers.
This being said, there are cases where using the session is the right thing to do. Without becoming too political, you can think it the session as a gun: there are, sometimes, good uses for it, but even a skilled practitioner is well advised to keep it under locked doors ;).
You store and retrieve XML to/from the session with two functions: xxforms:get-session-attribute()xxforms:set-session-attribute()
For instance: - The following replaces the content of the
from-session instance with the XML document stored in the session under the key session-howto. Note how xforms:insert replaces the whole instance when the nodeset expression points to the root element of the instance.
<xforms:insert nodeset="instance('from-session')"
origin="xxforms:get-session-attribute('session-howto')"/>
- The following stores the content of the
ui instance in the session under the session-how key. All you need to do here is to call the xxforms:set-session-attribute(), but since there is no construct in XForms to "just call an XPath function", you can use the xforms:insert action: it will evaluate the expression you specified in the origin attribute, and insert what it returns in the node context points to. Since the xxforms:set-session-attribute() returns an empty sequence, your instance won't be modified.
<xforms:insert context="." origin="xxforms:set-session-attribute('session-howto', instance('ui'))"/>
- The following removes what is stored in the session under the
session-howto key.
<xforms:insert context="." origin="xxforms:set-session-attribute('session-howto', ())"/>
Run it and get the source
|