6.3 Number Functions


This section defines the various number functions provided by the XForms core function library. These functions were added to the XForms core because they were missing in the XPath core but deemed necessary for typical form-based applications. Some of these functions are being adopted by the XPath working group for inclusion in XPath 2.0.

6.3.1 Computing Minimum, Maximum, and Average

The XPath core function library defines a number of functions like sum that reduce a node-set by applying a given mathematical operation. XForms extends these with the following functions:

avg

Function avg computes the average value from a node-set. This average is computed by

  1. Converting the string representation of each node in the node-set to a number,

  2. Reducing the resulting set of numbers by applying function sum to sum these values, and

  3. Dividing this sum by the number of elements in the node-set as computed by function count .

min

Function min computes the minimum value given a node-set. This minimum is computed by

  1. Converting the string representation of each node in the node-set to a number and

  2. Reducing this set by computing the minimum value.

max

Function max computes the maximum value given a node-set. This maximum is computed by

  1. Converting the string representation of each node in the node-set to a number and

  2. Reducing this set by computing the maximum value.

In Figure 6.2, we illustrate the use of these functions with a fragment from a census report.

Figure 6.2 Functions min , max , and avg used in a census report.
 <  html   xmlns  ="http://www.w3.org/1999/xhtml"  xmlns:xf  ="http://www.w3.org/2002/xforms">   <  head  ><  title  >Census Report</  title  >     <  xf:model   id  ="data">       <  xf:instance   xmlns  ="">         <  census  >           <  state  ><  population  /></  state  >       </  census  ></  xf:instance  >   </  xf:model  ></  head  >   <  body  >     <  xf:group   xmlns  ="http://www.w3.org/2002/xforms">       <  output   value  ="min(/census/state/population)">       <  label  >Smallest  population</  label  ></  output  >       <  output   value  ="max(/census/state/population)">       <  label  >Largest  population</  label  ></  output  >       <  output   value  ="avg(/census/state/population)">       <  label  >Avg state population</  label  ></  output  >   </  xf:group  ></  body  > </  html  > 

6.3.2 Function index for Addressing repeat Structures

Function index takes a string argument that is the id of a repeat structure. It returns the current value of the repeat index for that repeat structure. We used function index in creating add, remove, and scroll controls in Figure 4.13 and Figure 4.23.

6.3.3 Function count-non-empty

The XPath core contains function count that returns the number of nodes in a node-set. XForms adds a count-non-empty function, which first reduces the input node-set by removing all empty nodes before applying function count . Here, a node is said to be empty if and only if the string representation of the contents of that node has length 0.



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