XML
  Home arrow XML arrow Page 5 - XForms Basics, Part 3
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

XForms Basics, Part 3
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 14
    2004-01-19

    Table of Contents:
  • XForms Basics, Part 3
  • Operating with Extreme Caution
  • Money, Money, Money
  • Shop 'till You Drop
  • The Bookworm Turns
  • An Event to Remember
  • Link Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    XForms Basics, Part 3 - The Bookworm Turns
    (Page 5 of 7 )

    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"
    xmlns
    :xforms="http://www.w3.org/2002/xforms/cr"
    xmlns
    :xsd="http://www.w3.org/2001/XMLSchema">
    <head>
    <!-- define the form model -->
    <xforms:model id="bookstore">
     
    <xforms:instance>
      
    <inventory>
       
    <books
     
        <
    title>Be Cool</title>
        
    <author>Elmore Leonard</author>
        
    <price>7.97</price>
        
    <quantity>150</quantity>
     
        <
    title>Mystic River</title>
        
    <author>Dennis Lehane</author>
        
    <price>25.00</price>
        
    <quantity>86</quantity>
     
        <
    title>Hit List</title>
        
    <author>Lawrence Block</author>
        
    <price>23.76</price>
        
    <quantity>26</quantity>
     
        <
    title>Silent Joe</title>
        
    <author>TJefferson Parker</author>
        
    <price>24.99</price>
        
    <quantity>268</quantity>
     
        <
    title>The Travel Detective</title>
        
    <author>Peter Greenberg</author>
        
    <price>34.87</price>
        
    <quantity>9</quantity>
        

       
    </books
     
       <
    totalInventory />
     
       <
    mostExpensive />
     
       <
    leastExpensive />
     
       <
    averageCost />
      
    </inventory>    
     
    </xforms:instance>
     

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

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

    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">
     
    <xforms:instance>
      
    <calc>
       
    <dt />
       
    <numDays />
      
    </calc>
     
    </xforms:instance>
    <xforms:bind nodeset="/calc/numDays" calculate="days-from-date(/calc/dt)" />
    </xforms:model>

    Pretty straightforward, isn't it?

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


     

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway