The Secret Life of XForms : Page 3

XForms recently reached the W3C's Candidate Recommendation statusand you need to know about itbecause XForms isn't a form description language, it's a language for describing applications in a platform-independent way. Best of all, it integrates easily with technologies you already know, such as XHTML, XPath, SVG, and CSS.

WEBINAR:

On-Demand

Building a Model
The second level of abstraction tied into XForms is the creation of models. A model represents an internal entity into which the interface (through the components) provides a window. Programming languages such as C# or Java usually reflect the model using classes and the relationships between classes.

However, one of the benefits of working with XML is that it is generally much easier to create an internal model as an XML document than as a set of related classes. The user interface may not necessarily duplicate the model exactly. For example, in the stock example shown earlier, while the internal model represented the price of each stock and the final price as an integer, the interface showed the price as a dollar amount45225.253 became $45,225.25. The user interface may hide some items or display computed quantities based upon the contents the internal model.

The <xforms:model> element contains the model. At the topmost levels this looks like:

The model's id identifies the model in subsequent expressions (invoked via the model attribute). The XForms engine automatically assumes that the first model in the document is the reference document unless explicitly overridden by a model attribute. Each model in turn contains instance data.

Everything within the instance should be valid XML. This document is the state repository for the XForm. That differs from other XML applications because it's dynamicvalues within the instance can change in response to events that occur in the XForms application.

The example gives two distinct pieces of information, combined for convenience. The first part contains the relevant stock data for five stocksthe stock name, symbol, and price per share of the stock. In this example, the information is static. In a working application you could load the information dynamically from an external resource.

The elements <name>, <symbol>, <value>, <shares>, and <totalCost> act as temporary variables that will be assigned values based upon the information from the components. This information is dynamicchoosing a new stock sets a new symbol and value, which then interact with the shares value to determine total costs. In essence, these become the equivalent of private variables, accessible only through the interface.

If you run the example XForm, set the drop-down box (Available Stocks) to Allied Data Systems and the number of shares to 12, then the internal model changes to:

Note that this latter version doesn't contain any of the formatting information shown in the display. The forms engine handles the appearance of content while the internal model contains the base data used to generate the content. In that respect, XForms is much like XSLT, except that where XSLT creates an entirely new output based upon the XML input, XForms simply modifies the XML input directly.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date