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

The Bookworm Turns - 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.

  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



In addition to simple comparison tests, you can also use XPath arithmetic and non-arithmetic functions in an XForms model. Consider the following example:

<html xmlns="http://www.w3.org/1999/xhtml"
<!-- define the form model -->
<xforms:model id="bookstore">
title>Be Cool</title>
<author>Elmore Leonard</author>
title>Mystic River</title>
<author>Dennis Lehane</author>
title>Hit List</title>
<author>Lawrence Block</author>
title>Silent Joe</title>
<author>TJefferson Parker</author>
title>The Travel Detective</title>
<author>Peter Greenberg</author>

totalInventory />
mostExpensive />
leastExpensive />
averageCost />

<xforms:bind nodeset="/inventory/books/totalInventory"
="sum(/inventory/books/quantity)" /> 
<xforms:bind nodeset="/inventory/books/mostExpensive"
="max(/inventory/books/price)" />
<xforms:bind nodeset="/inventory/books/leastExpensive"
="min(/inventory/books/price)" />
<xforms:bind nodeset="/inventory/books/averagePrice"
="avg(/inventory/books/price)" />

<!-- define the form interface -->
<!-- loop over the instance data -->
<xforms:repeat nodeset="/inventory/books">
<xforms:input id="txttitle" ref="title">
<xforms:input id="txtauthor" ref="author">
<xforms:input id="txtprice" ref="price">
<xforms:input id="txtquantity" ref="quantity">
<!-- print 
calculated values -->
Total inventory
: <xforms:output ref="/inventory/books/totalInventory" />
<br />
Price of most expensive book: <xforms:output
="/inventory/books/mostExpensive" /> <br />
Price of least expensive book: <xforms:output
="/inventory/books/leastExpensive" /> <br />
Average price
<xforms:output ref="/inventory/books/averagePrice" />

As usual, all the action is centered around the definition of the XForms model. This time, the instance data has been populated with the inventory of the neighbourhood bookstore, and XPath functions like sum(), min(), max() and avg() have been used to perform calculations on that data, and assign the results of those calculations to specific summary elements in the instance data. This data is then displayed in the form using the
<xforms:output> element.

You'll also notice a new element in the form above - the <xforms:repeat> element. This element gives XForms authors the ability to loop over a collection of nodes without having to resort to complex "while" or "for" loops. In this example, the <xforms:repeat> element causes the XForms processor to iterate over the <books> collection from the instance data, and display the values of the title, author, price and quantity. The "nodeset" attribute specifies the node over which iteration should take place, while the loop counter is handled internally by the processor. Looping will stop once no further match is found for the "nodeset" criteria.

Why stop just at numeric functions, though? This next example uses the days-from-date() function to calculate the number of days between an entered date and January 01 1970.

The XForms model will look something like this:

<!-- form model -->
<xforms:model id="dateCalculator">
<dt />
<numDays />
<xforms:bind nodeset="/calc/numDays" calculate="days-from-date(/calc/dt)" />

Pretty straightforward, isn't it?

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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


Dev Shed Tutorial Topics: