Thursday, April 28, 2011

New XPath functions to get a control's current label, help, hint, or alert

In XForms 1.1, a control's label, help, hint, or alert (or LHHA as we call them at Orbeon) can read its value from data, in particular using the ref attribute. But the converse is not possible: for example if a control has a given label, you cannot figure out the value of that label from an XPath expression.

So we have just implemented in Orbeon Forms the following convenient functions:

Usage is very simple. In this example, when the user presses the button, the current value of the label is displayed in a message:


This raised a small design question: is it better to write xxf:label('id'), or xxf:lhha('id', 'label')? We have opted for the former, which is more explicit and better typed. Also, it doesn't seem like many more similar control properties will be added in the future.

The functions are also documented on the Orbeon Forms wiki.

For the curious, the implementation of these 4 functions is really simple in a single short Scala class, and it plugs very nicely into the Saxon XPath engine:

Note the uses of:

  • pattern matching: case control: XFormsControl if control.isRelevant =>
  • a nested function: evaluateControlItem is local to evaluateItem
  • first-class functions: XFormsControl.getLabel, etc. are passed as a parameter to evaluateControlItem
In this case, the implementation would probably not have been outrageously different in Java: we would have used if instead of match, a private method instead of a local function, and passed one of the LHHA values to that private method instead of passing functions around.

No comments:

Post a Comment