6.2 Boolean Functions


This section defines the various boolean functions provided by the XForms core function library.

6.2.1 Function boolean-from-string

Function boolean-from-string was added to the XForms core to ease the use of XML Schema's xsd:boolean type and to assist in correctly mapping the string representation of xsd:boolean to the appropriate logical value. XML Schema defines the string representations for the logical values true and false as shown in Table 6.1. XPath in turn represents logical true as true() and logical false as false() .

Table 6.1. String Representation of Legal xsd:boolean Values

Logical Value

xsd:boolean

XPath

true

1

true()

 

true

 

false

false()

 

false

 

Function boolean-from-string converts an xsd:boolean value to the correct XPath boolean value. This function is case-insensitive with respect to its argument and throws an error if called with an illegal xsd:boolean value. We used function boolean-from-string in Figure 4.26 when counting the number of open tasks with the expression

 
 "count(//task[not(boolean-from-string(done))])" 

The XPath boolean value returned by function boolean-from-string is negated via a call to XPath core function not . This predicate is in turn used to filter the set of nodes returned by XPath locator //task , and the resulting node-set is passed to XPath core function count .

6.2.2 Function if

XForms model property calculate enables the creation of instance data that is automatically computed as the user interacts with portions of a form. This functionality is often used to implement simple spreadsheet-like interfaces. In creating such interfaces, a common use case is to test for a specific condition and return one of two values depending on the value returned by the test condition.

XForms defines function if to enable this use case. Function if takes three string arguments; it evaluates the first argument as an XPath expression to compute a boolean value. If this evaluates to true() , the result of evaluating the second argument is returned; if it evaluates to false() , the function returns the result of evaluating its third argument.

Note that if is a function and not an if statement as found in programming languages like C or Java. This distinction is important to ensure that the expression language used in XForms does not turn into a programming language of its own. We use function if to compute the cost of shipping, as shown in Figure 6.1. For orders of $100 or more, shipping is free. For orders of less than $100, the user is charged a fixed percentage of the total cost for shipping and handling.

Figure 6.1 Using function if to compute shipping cost.
 <  model   id  ="iv"  xmlns  ="http://www.w3.org/2002/xforms"> <  instance  > <  invoice   xmlns  =""> <  items  ><  item  />...</  items  > <  cost  /><  shipping  /><  total  /> </  invoice  ></  instance  > <  bind   nodeset  ="/invoice"> <  bind   nodeset  ="total"  calculate  ="cost+shipping"/> <  bind   nodeset  ="shipping"  calculate  = 'if(cost &gt; 100.00, "0.0", "cost * 0.05")'/> </  bind  > </  model  > 


XForms. XML Powered Web Forms with CD
XForms. XML Powered Web Forms with CD
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 94

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net