Search code examples
xqueryorbeonxforms

XForms: Complex calculation for binding


I am looking for a possibility to perform complex calculations for a binding. The following instance is given:

<root>
  <appointments>
    <appointment><date>2012-01-01</date></appointment>
    <appointment><date>2012-01-03</date></appointment>
  </appointments>
  <weeks />
</root>

The weeks node depends on the appointment nodes:

$weeks = 0
$week_begin = xs:date("1970-01-01")
for $appointment in //appointments/appoinment
  if # Check if $appointment is in new week
    $weeks = $weeks + 1
    $week_begin = # Do some more calculationx

My problem is that I don't know where to "put" those calculations. As you can see we need a loop with variables that can store some information between loop cycles. Therefore, I have evaluated the following options:

  1. xforms:variable in combination with xforms:repeat => xforms:variable inside a repeat can't write to the "outside" scope
  2. xforms:setvalue in combination with an extra instance that holds temporary variables and xforms:repeat
  3. xforms:bind with calculate attribute => for loops in xpath are possible, but no variables

Any advice how to approach this problem? Thank you!


Solution

  • You could try to implement the calculation in Javascript that's triggered if the appointments nodeset has changed. If you're using Orbeon Forms, you could use the xxforms:script extension. The Orbeon wiki has an example how to set an instance value based on a javascript calculation.

    Another solution would be to completely "externalise" the calculation and to create a little web service (XQuery?) that would return the result of the calculation. You could use the xforms:submit mechanism to feed that service and access its result.