| home / programming / xforms / | [previous][next] |
|
|
The next step is to create an XForms document that will serve to edit the initial instance data. XForms itself does not define a document format. Instead, a host language such as XHTML or SVG, combined with XForms, needs to be used. As of this writing, XHTML 2.0, which natively includes XForms, is progressing through the W3C Recommendation track. This example, however, uses the established XHTML 1.1, with XForms elements inserted in the appropriate places. As a result, this example will not validate against any XHTML DTD. Even so, it is still XML well-formed, and browsers that understand XForms presently do a good job rendering this document.
The latter part of this chapter describes complications that occur when combining vocabularies; the opening lines of the XForms document shown in Example 2-2 provide a foregleam, using an arcane XML syntax called an internal DTD subset to declare certain attributes as document-unique IDs.
Example 2-2: Opening lines of an XForms document
<?xml version="1.0"?><?xml-stylesheet type="text/css" href="style.css" ?><!-- the following extremely ugly code is necessaryto make ID attributes behave as expected --><!DOCTYPE html [<!ATTLIST object id ID #IMPLIED><!ATTLIST model id ID #IMPLIED><!ATTLIST bind id ID #IMPLIED><!ATTLIST instance id ID #IMPLIED><!ATTLIST submission id ID #IMPLIED><!ATTLIST group id ID #IMPLIED><!ATTLIST repeat id ID #IMPLIED><!ATTLIST case id ID #IMPLIED>]><html xmlns="http://www.w3.org/1999/xhtml"xmlns:ev="http://www.w3.org/2001/xml-events"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:u="urn:oasis:names:tc:ubl:CommonAggregateTypes:1.0:0.70"xmlns:xforms="http://www.w3.org/2002/xforms"><head><title>Create a Purchase Order</title>
After the usual XML declaration, the document starts out with
a reference to a CSS file to provide style information. Next, the DOCTYPE
declaration and the several ATTLIST statements
are necessary to make sure that the several ID-typed attributes that will be
used are actually treated as IDs.
Following that is the beginning of a normal html
element, with several namespace declarations that will be used later in the
document. Last is the standard HTML head element,
with a title.
The next several lines, in Example 2-3, make up the XForms Model--essentially everything there is to know about the form other than how it will look or otherwise be rendered.
Example 2-3: Starting the XForms Model
<xforms:model id="default"><!-- schema="schema.xsd" --><xforms:instance src="ubl_samp.xml"/><xforms:submission action="file://tmp/ubl.xml" method="put" id="submit"/><!-- a few things are always required --><xforms:bind nodeset="u:IssueDate" required="true( )" type="xs:date"/><xforms:bind nodeset="u:OrderLine/u:Quantity" required="true( )"type="xs:nonNegativeInteger"/><xforms:bind nodeset="u:OrderLine/u:Item/u:BasePrice/u:PriceAmount"required="true( )" type="xs:decimal"/><xforms:bind nodeset="u:OrderLine/u:Item/u:SellersItemIdentification/u:ID"required="true( )"/><!-- a few basic calculations --><xforms:bind nodeset="u:OrderLine/u:LineExtensionAmount" type="xs:decimal"calculate="../u:Quantity * ../u:Item/u:BasePrice/u:PriceAmount"/><xforms:bind nodeset="u:LineExtensionTotalAmount" type="xs:decimal"calculate="sum(../u:OrderLine/u:LineExtensionAmount)"/>
The xforms:model element is the container
for the entire XForms Model. In a document with only one such element, an id
attribute isn't strictly needed, though it is good practice to always include
one. With the addition of the attribute schema="UBL_Library_0p70_Order.xsd"
it would be possible to associate a pre-existing XMLSchema with this form, though
that option is commented out here. XML Schema processing would add significant
overhead, and the few places that require additional datatype information can
be easily specified separately. The xforms:instance
element, with the src attribute, points to the
initial instance data that was listed earlier. The xforms:submission
element indicates that activating submit on this form will write XML to the
local file system.
The next several lines contain xforms:bind
elements, each of which selects a specific part or parts of the instance data,
applying various XForms properties to the selection. The language used to select
the XML parts, or nodes, is called XPath, which is
perhaps better known as the selection language used in XSLT, XPointer, and XML
Signature. The next chapter describes XPath in detail. XForms includes defaulting
rules that simplify most of the XPath selection expressions, declared on the
nodeset attribute, and called model
binding expressions. The first model binding expression selects the one-and-only
u:IssueDate instance data node, marking it as required
and of the XML Schema datatype date, which provides the hint that this particular
data should be entered with a date-optimized form control, such as a calendar
picker. The second model binding expression applies to however many u:Quantity
elements happen to exist at any given time, and marks all of them as requiring
user entry, along with the XML Schema datatype xs:nonNegativeInteger.
The next few model binding expressions set up the two calculations
that are fundamental to a purchase order: calculating the total amount for a
line item (price times quantity), and the total for the whole order (sum of
all line items). The calculate attribute holds an XPath expression that gets
evaluated to determine a new value for the node to which it is attached. The
calculation for line items is ../u:Quantity * ../u:Item/u:BasePrice/u:PriceAmount,
where the asterisk means multiply, and the operands on either side of it are
path expressions, relative to the u:LineExtensionAmount
element. In turn, the calculation for the grand total is sum(../u:OrderLine/u:LineExtensionAmount),
which uses the function sum( ) to add up all the
values from individual u:LineExtensionAmount nodes.
Like a spreadsheet, recalculations will occur whenever needed, and dependencies
among calculations will automatically be handled in the correct order. For example,
individual line items will always be multiplied out before the overall total
is summed up.
The definition of the XForms Model continues with the lines in Example 2-4.
Example 2-4: The rest of the XForms Model
<!-- a second instance, temporary data not to be submitted --><xforms:instance id="scratchpad"><temp xmlns=""><currencyOptions><option value="EUR">Euro</option><option value="GBP">Pound</option><option value="USD">Dollar</option></currencyOptions></temp></xforms:instance><!-- global setting of currencyID --><xforms:bind nodeset="u:OrderLine/u:LineExtensionAmount/@currencyID"calculate="../../u:LineExtensionTotalAmount/@currencyID"/><xforms:bind nodeset="u:OrderLine/u:Item/u:BasePrice/u:PriceAmount/@currencyID"calculate="../../../../u:LineExtensionTotalAmount/@currencyID"/></xforms:model></head
| home / programming / xforms / | [previous][next] |
Created: March 27, 2003
Revised: October 10, 2003
URL: http://webreference.com/programming/xforms/1