Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.


advertisement
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:

<xforms:model id="userAccounts"> <xforms:instance> <stockData> <stocks> <!-- a listing of stock data --> </stocks> <account> <!-- account information </account> </stockData> </xforms:instance> </xforms:model>

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.

<stocks> <stock> <name>Allied Data Systems</name> <symbol>ADS</symbol> <value>25.00</value> </stock> <stock> <name>Beta System Components</name> <symbol>BSC</symbol> <value>17.37</value> </stock> <!-- additional stock data ( </stocks>

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 second part of the model, however, provides the working core.



<account> <stock> <name/> <symbol/> <value>0.00</value> </stock> <shares>0</shares> <totalCost>0.00</totalCost> </account>

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:

<account> <stock> <name>Allied Data Systems</name> <symbol>ADS</symbol> <value>25.00</value> </stock> <shares>12</shares> <totalCost>300.00</totalCost> </account>

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap