It is often useful to load some XML data, place it into a form, save changes back to the XML document as they are made in the form, and post the XML back to the server.
Having loaded some XML into a global variable $userProfile:
<?xml version="1.0" encoding="UTF-8"?>
<UserProfile>
<Name first="Roger" surname="Rabbit" />
<Demographics age="25" />
</UserProfile>
<UserProfile>
<Name first="Roger" surname="Rabbit" />
<Demographics age="25" />
</UserProfile>
we want to bind a field to an element in the XML document using an xpath:
<input type="text" id="firstName"
path="$userProfile/Name/@first" e:behavior="l:autofill" />
path="$userProfile/Name/@first" e:behavior="l:autofill" />
When the field value changes, the XML attributes are updated. Here is the behavior:
<d:tdl>
<d:namespace name="http://www.backbase.com/2007/labs">
<d:behavior name="autofill">
<d:attribute name="path" />
<d:handler event="populate" type="application/javascript">
var path = bb.command.getAttribute(this, 'path');
this._data = bb.evaluateSmart(path, bb.document)
var sFirst = this._data.value;
bb.setProperty(this, 'value', sFirst);
</d:handler>
<d:handler event="change" type="application/javascript">
this._data.textContent = bb.getProperty(this, 'value');
</d:handler>
</d:behavior>
</d:namespace>
</d:tdl>
<d:namespace name="http://www.backbase.com/2007/labs">
<d:behavior name="autofill">
<d:attribute name="path" />
<d:handler event="populate" type="application/javascript">
var path = bb.command.getAttribute(this, 'path');
this._data = bb.evaluateSmart(path, bb.document)
var sFirst = this._data.value;
bb.setProperty(this, 'value', sFirst);
</d:handler>
<d:handler event="change" type="application/javascript">
this._data.textContent = bb.getProperty(this, 'value');
</d:handler>
</d:behavior>
</d:namespace>
</d:tdl>
This is very basic proof of concept code (attached). To do:
* handle updating attributes and element text content
* handle binding for elements that do not have a 'value' property
| Attachment | Size |
|---|---|
| dataform.zip | 2.03 KB |

Comments
Thanks Richard for this
31 October, 2007 - 14:49 — Paweł KatarzyńskiThanks Richard for this post, it helped a lot as in my case, XML in and XML out is most desired behavior for my forms (I'm using eXist XML database). Do You intent to build-in more general mechanism for XML databinding in the future releases of Backbase?