Home arrow XML arrow Page 6 - XForms Basics, Part 3

An Event to Remember - XML

XMLIn the previous article, I showed you how to manage user input in the XForms model. I discussed the process of submitting an XForm and – more importantly - validating user input prior to submission using built-in XML Schema support. In this concluding article, find out how to use the <xforms:bind> element to perform calculations on form input values, integrate XPath expressions into your XForms model and get a crash course in the XForms event model.

TABLE OF CONTENTS:
  1. XForms Basics, Part 3
  2. Operating with Extreme Caution
  3. Money, Money, Money
  4. Shop 'till You Drop
  5. The Bookworm Turns
  6. An Event to Remember
  7. Link Out
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 14
January 19, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

It was the introduction of events in client-side scripting languages like JavaScript and VBScript that brought about the rise of the dynamic Web site. However, inconsistencies in implementation across browsers meant that developers had to grapple with multiple lines of code to make Web sites cross-browser compatible. XForms has put an end to this misery, at least so far as form events are concerned.

The XForms 1.0 specification divides the process of event handling into four phases:

Initialization: This is the very first stage, wherein the XForms processor "wakes up" and begins construction of the data model using the instance data provided. This is also the stage where all relevant XML Schemas are loaded, and all form controls (with their associated bindings) are initialized.

Interaction: Interaction events are fired as a result of action from the user - for example, keyboard navigation to a new input control, mouse clicks, data entry or item selection. A number of different events can occur in this phase, and each one has a default action associated with it.

Notification: Notification events don't usually have a default action associated with them; rather, they're triggered as a result of a change in the form state, such as a form control receiving focus or a button being clicked.

Error handling: These events occur due to errors in XForm processing, such as illegal binding expressions or illegal XPath references. Errors may be either fatal or non-fatal, depending on their severity.

OK, now enough of the theory. Let's look at a simple example:

 
<!-- define the form model --> 
<xforms:model id="bankAccount"
 
<xforms:instance
  
<account
   
<openingBalance>10000</openingBalance
   
<withdrawal /> 
   
<closingBalance /> 
  
</account
 
</xforms:instance>
 <
xforms:bind id="nodeClosingBalance" 
nodeset
="/account/closingBalance" /> 
</
xforms:model>
<!-- 
define the form interface --> 
<xforms:input id="txtwithdraw" ref="/account/withdrawal"
 
<xforms:label>Amount to withdraw</xforms:label
 
<xforms:hint>Please enter the amount you wish to
 withdraw
</xforms:hint
</xforms:input>
<
xforms:trigger
<xforms:label>Calculate Closing Balance</xforms:label
<xforms:setvalue ev:event="DOMActivate" 
bind
="nodeClosingBalance" 
value
="/account/openingBalance - /account/withdrawal" />
</xforms:trigger

By definition, the <xforms:trigger> element is the XForms counterpart of the regular HTML form button. When clicked, it generates a DOMActivate event, which is a catch-all event type fired whenever any event (pressing a button, selecting an option) occurs. The <xforms:setvalue> element, which is used to set the value for a particular node after computing it from an XPath expression, listens for this event and acts when it receives it. In this case, the action involves subtracting the withdrawal amount from the opening balance to obtain a new closing balance.

A number of other event types are available in the XForms model - you've already seen one of the other important ones, the xforms-submit event, in the previous segment of this tutorial. There are too many to list here, so you should take a look at the specification (and the excellent examples included within it), to better understand this topic.



 
 
>>> More XML Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: