RE: xforms:choose/if?

Alexander,

The only way you can do this *today* is with a little bit of script (see
below).

For the future, some work has been done in XML Events 2 to provide
conditional event handlers, which would do pretty much what you want. The
syntax will probably be something like this:

  <xf:send
   ev:event="DOMActivate"
   ev:condition="instance('first_step')//count_result = 0"
   submission="ws_1"
  />

  <xf:send
   ev:event="DOMActivate"
   ev:condition="instance('first_step')//count_result = 1"
   submission="ws_2"
  />

etc.

To do this today though, a script solution is possible, but it will
obviously depend on your processor as to what exactly the code looks like,
or whether it is supported at all. Which means that for now, any solution is
unlikely to be cross-platform.

I'll show how it would be done in formsPlayer, since the principles will be
the same in any processor that supports DOM 2 Events [1]. If your chosen
processor does support the creation of DOM 2 Event objects in script, then
you can easily modify the following code.

First, set up handlers that invoke your submissions:

  <xf:trigger id="tr">
    <xf:label>Send</xf:label>
    <xf:send ev:event="my-submit-1" submission="ws_1" />
    <xf:send ev:event="my-submit-2" submission="ws_2" />
  </xf:trigger>

Next, create a script that tests your chosen node, and then dispatches
different events to the xf:trigger, based on your test (note that this would
most likely be done with some sort of array of value-to-event-name mappings,
or using switch):

  <script for="tr" event="DOMActivate">
    var oModel = document.getElementById("m");
    var oInstance = oModel.getInstanceDocument("first_step");
    var oNode = oInstance.selectSingleNode("//count_result");
    var c = oNode.text;

    /* create the event name, somehow or other */
    var sEventName = "my-submit-" + (c + 1);

    /* create an Event object, ready to be dispatched */
    var oEvent = new ActiveXObject("DOM2Events.DOMEvent");

    /* this is standard */
    oEvent.initEvent(sEventName, false, false);
    tr.dispatchEvent(oEvent);
  </script>

Obviously not all XForms processors support DOM 2 Events in script, but for
those that do, the only line in the above that is 'non-standard' is the part
that creates an 'Event' object:

  var oEvent = new ActiveXObject("DOM2Events.DOMEvent");

This creates a DOM 2 Events 'Event' object that can then be initialised via
a standard call to initEvent(), before being passed to the xf:trigger.

As it happens, even this script will shortly become more standard; in the
Sidewinder Viewer we have added new interfaces to
document.DOMImplementation, which means that you will shortly be able to
create the object something like this:

  var oDocumentEvent =
    document.DOMImplementation.getFeature("Events", "2.0");
  var oEvent = oFactory.createEvent("Event");

Since this is a sequence of standard DOM calls to obtain standard
interfaces, your scripts would then work in Mozilla, Sidewinder/formsPlayer
and any other browser/processor combination that uses
'document.DOMImplementation'.

Anyway, I hope that gives you an idea of where the issue stands in both the
short-term (scripting) and long-term (conditional event handlers).

Regards,

Mark

[1] <http://www.w3.org/TR/DOM-Level-2-Events/>


Mark Birbeck
CEO
x-port.net Ltd.

e: Mark.Birbeck@x-port.net
t: +44 (0) 20 7689 9232
w: http://www.formsPlayer.com/
b: http://internet-apps.blogspot.com/

Download our XForms processor from
http://www.formsPlayer.com/ 

> -----Original Message-----
> From: www-forms-request@w3.org 
> [mailto:www-forms-request@w3.org] On Behalf Of Alexander Anokhin
> Sent: 31 March 2005 05:30
> To: www-forms@w3.org
> Subject: Re: xforms:choose/if?
> 
> 
> Alessandro Vernet wrote:
> > Hi Alexander,
> > 
> > Could you clarify what you mean by "Now i need to do 
> something (toggle 
> > some switch, send some data somewhere, etc..) depends on received 
> > number."?
> > 
> > You can compute the integer with a bind/calculate and store 
> it in the 
> > instance.
> 
> My integer is an sql count(*) operation result from database, 
> for example and i can't compute it locally.
> 
>    Then if you want to display or hide something in the view based
> > on that he can use a bind/relevant. But I am not sure this 
> is what you 
> > are looking for.
> 
> I don't wand to show/hide something (with CSS & 
> bind/relevant) but do some action. The wanted feature like in XSLT:
> 
> <xforms:action ev:event="DOMActivate">
>       <xforms:choose>
>           <xforms:when 
> test="instance('first_step')//count_result = 0">
>               <xforms:send submission="ws_1"/>
>           </xforms:when 
> test="instance('first_step')//count_result = 1">
>           <xforms:when test="instance('first_step')">
>               <xforms:send submission="ws_2"/>
>           </xforms:when>
>           <xforms:otherwise">
>               <xforms:toggle case="case3"/>
>           </xforms:otherwise>
>        </xforms:choose>
> </xforms:action>
> 
>    Just let us know, so people on the list can elaborate
> > further.
> 
> Thanx for answer anyway and sorry for my casual english.
> 
> 
> > --- Alexander Anokhin <ava@vaz.ru> wrote:
> > 
> >>Hi, all...
> >>Q: How can i achieve some kind of "choose/if" functionality 
> in XForms?
> >>The use case is:
> >>1. User input some data and press "Proceed"; 2. XForms send it to 
> >>server (with replace="instance"); 3. The answer is a some integer 
> >>number; 4. Now i need to do something (toggle some switch, 
> send some 
> >>data somewhere, etc..) depends on received number.
> >>
> >>Is it possible?
> 
> --
> Alexander Anokhin
> AVTOVAZ JSC
> email: ava@vaz.ru
> icq: 123275798
> 
> 

Received on Thursday, 31 March 2005 12:16:16 UTC