ewval ) { if (node.firstChild) { node.firstChild.nodeValue = newval; } else { var textnode = node.ownerDocument.createTextNode( newval ); node.appendChild( textnode ); } } function sum(xpath) { var nodes = XDocument.DOM.selectNodes(xpath); var total = 0; for (var idx=0; idx<nodes.length; idx++) { total = total + parseFloat(getElementValue(nodes[idx])); } return total; } function floatToString(value) { return "" + value; }
This script approach requires a bit of care in getting and setting values from XML elements. In accordance with the DOM worldview, actual data values are stored in a text node child of the element node, except that an empty value is signified by the lack of any text child node.
On my initial attempt at this script, I neglected to attach an OnLoad entry point. The solution seemed to work, but had a subtle bug: if the initial value in the currency selection list was never changed, the currency value never got propagated throughout the XML. One advantage of a declarative approach is that it applies equally to initial conditions as well as the ongoing state. The XForms example didn't have to be special-cased for initialization.
Though not used here, the extensive sample forms that come with InfoPath include a large library of script that can be reused via copy-and-paste. The resulting InfoPath document can be filled out in the same application that designed the form, as shown in figure 2.
|
| The data entry view of Microsoft InfoPath (click for larger image) |