XML
  Home arrow XML arrow Page 4 - XForms Basics
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 41
    2004-01-12


    Table of Contents:
  • XForms Basics
  • Out with the Old...
  • ... In With the New
  • What's In A Name?
  • Welcome to Nowhere
  • A Tour of Nowhere

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    XForms Basics - What's In A Name?
    ( Page 4 of 6 )

    Let's start with a simple example that demonstrates the functionality of XForms.


    <html xmlns=http://www.w3.org/1999/xhtml
     xmlns:xforms="http://www.w3.org/2002/xforms/cr"> 
     
    <head>
     
    <!-- 
    define the form model -->
    <
    xforms:model id="enter">
     <
    xforms:instance>
      <
    user>
       <
    name />
      </
    user>
     </
    xforms:instance>
    </
    xforms:model>
     
    <
    basefont face="Arial">
    </
    head>
     
    <
    body>
    <
    font size="+1">
    What's In A Name?
    </font><br /><br />
     
    <!-- define the form interface -->
    <xforms:input id="txtname" 
    model="enter" ref="/user/name">
     <xforms:label>Name</xforms:label>
     <xforms:hint>
     Enter your name, then press TAB
     </xforms:hint>
    </xforms:input>
     
    <br />
     
    <!-- do something with the input -->
    Welcome to XForms, 
    <xforms:output model="enter" 
    ref="/user/name" />
     
    </body>
    </html>

    Let's look at this line by line.

    The first basic rule when dealing with XForms is that they cannot exist as independent entities; instead, they're designed to be integrated with XHTML (or other markup) documents. This is why the XForm above is enclosed within a regular XHTML document, and why the XHTML namespace has been referenced at the top.


    <html xmlns=http://www.w3.org/1999/xhtml
    xmlns:xforms="http://www.w3.org/2002/xforms/cr"
    ...
    </
    html>

    Note that in addition to the XHTML namespace, when dealing with XForms, you'll also need to specify the XForms namespace, which is currently "http://www.w3.org/2002/xforms/cr".


    <!-- define the form model -->
    <
    xforms:model id="enter">
    ...
    </
    xforms:model>

    The second basic rule when working with XForms is this: every XForm consists of two primary components, a form model (which specifies functionality) and one or more form controls (which handle presentation).

    The form model, indicated by the <xforms:model> element, defines what the form does. This model defines the data to be captured/modified by the form as well as the logical components that govern how the form behaves on user interaction (including where the form should be submitted and how to handle the various form events). The model also includes information on the bindings between the form controls and the instance data, and can optionally link to external XML Schemas for validation purposes.

    The <xforms:model> element is usually accompanied with an "id" attribute, which contains a name for the model. This name is used by the form controls in order to link the interface section of the form to the correct form model. Obviously, this also means that you can have multiple models with a single XHTML file, and link to each one by its name. Additionally, since the form model only defines how the form works and has nothing to do with its presentation, it can easily be reused, either within the same application or in other applications or platforms.

    Typically, the <xforms:model> element appears within the <head> of the document, and encloses an <xforms:instance> element.


    <!-- define the form model -->
    <
    xforms:model id="enter">
     <
    xforms:instance>
      <
    user>
       <
    name />
      </
    user>
     </
    xforms:instance>
    </
    xforms:model>

    The <xforms:instance> element defines the "instance data" for the form This instance data is an XML tree representation of the values associated with the form, and it can be defined inline (as above) or imported from an external XML file. In this case, the XML structure is pretty simple: a single root element called <user> encompassing one child element called <name>.

    Normally, the <xforms:model> element would also contain information on where the form is to be submitted via the <xforms:submission> element, data bindings via the <xforms:bind> element and event handlers via the <xforms:action> element. I'll be discussing those shortly, so don't worry too much about them for the moment.

    Once the form model has been defined, it's time to define the other half of the puzzle: the form controls, or how the form looks. XForms provides a bunch of elements to accomplish this; by and large, they map neatly into the HTML form controls you're already familiar with, and appear within the <body> of the document.


    <!-- define the form interface -->
    <
    xforms:input id="txtname" 
    model="enter" ref="/user/name">
     <
    xforms:label>Name</xforms:label>
     <
    xforms:hint>
     
    Enter your namethen press TAB
     
    </xforms:hint>
    </
    xforms:input>

    The first (and simplest) of these controls is the <xforms:input> element, which represents a text entry field. Note my use of the "model" attribute to tell the XForms processor which form model this control belongs to, and the "ref" attribute to tell it which node in the instance data tree this particular value maps to. XPath expressions are used in the latter. In case you don't know what those are, take a look at http://www.melonfire.com/community/columns/trog/article.php?id=83 and they should make a little more sense to you.


    <!-- do something with the input -->
    Welcome to XForms
    <
    xforms:output model="enter" 
    ref="/user/name" />

    Another useful (though not often used) form control is the <xforms:output> element, which is used to display the value of a particular node from the XML instance data tree. Note again my usage of the "model" and "ref" attributes to link the control to the model and instance data tree defined in the <head> of the document.

    If you were to try viewing the XForm above in an XForms-capable browser, you'd see something like this:

    Pretty cool, huh?



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

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT