5

In designing an XForm interface to an XML database (using eXist and XSLTForms), I'd like to include an input control for an optional element. The XML data records already exist and while some contain the optional element, others don't. To update a record, I'm using the existing XML record as the model instance. The problem is that the form control is not displayed when the optional element is not present, which is logical, but presents a problem when a user wants to add data to the optional element.

To be more explicit, here's an example data record, data.xml:

<a>
  <b>content</b>
</a>

with RNC schema:

start =
  element a {
    element b { text },
    element notes { text }?
  }

XForms model:

<xf:model>
    <xf:instance xmlns="" src="data.xml"/> 
    <xf:submission id="save" method="post" action="update.xq" />
</xf:model>

And control:

<xf:input ref="/a/notes">
  <xf:label>Notes (optional): </xf:label>
</xf:input>  

The problem is that the 'Notes' input control is simply not displayed.

An obvious solution is to add a trigger button to allow the user to insert the element if needed, but it is preferable to just have the input control appear, and be empty.

My question is: Is there some subtle combination of lesser-known attributes/binds/multiple instances/xpath expressions that will cause the control to always be displayed?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Cam
  • 146
  • 1
  • 4

2 Answers2

2

To be honest, XForms doesn't handle this optional element situation very well. See this related discussion on Micah's blog. A not-so-satisfying workaround is to add empty elements for those optional elements after you retrieve the data from eXist, and to remove them before saving the data back to eXist.

avernet
  • 30,895
  • 44
  • 126
  • 163
  • 1
    Thanks for this suggestion. I have since found that adding _new_ elements with an insert is not easy. – Cam Jan 09 '11 at 04:44
  • 1
    Yes, the `xforms:insert` is a rather complicated construct, especially because of all the attributes you can use on that element. But there are 2-3 main use cases for it, each calling for different attributes, and once you get a hang of those things become simpler. You can find a sort of tutorial for the `xforms:insert` on: http://wiki.orbeon.com/forms/how-to/repeat-insert-position – avernet Jan 10 '11 at 18:54
2

This situation has already been discussed by the W3C Forms Group: http://www.w3.org/2010/07/07-forms-minutes.html Using new MIP could help to implement this in XSLTForms.

-Alain

Alain Couthures
  • 1,488
  • 9
  • 5
  • Alain, many thanks for XSLTForms! A fantastic resource. And thanks for this pointer to a possible future way to solve this. – Cam Jan 09 '11 at 04:45