5.3 Property relevant for Model-based Switching
Property relevant takes an XPath expression that is evaluated during user interaction to return a boolean value. By default, all nodes in the instance tree have this property set to true . When a node gets its relevant property set to false , user interface controls that bind to that node become unavailable to the user; that is, the user is not presented with these values. Property relevant enables the creation of dynamic applications that use model-based switching to create conditional user interfaces. Compare this to the interaction-based switching enabled by construct switch described in Section 4.2.
When property relevant is applied to a node, it applies to that node and all its descendant children. Thus, property relevant makes it possible to make entire subtrees in the instance become relevant . This can be used to advantage in making large portions of a complex user interface conditional upon the current state of the instance being populated by the user.
5.3.1 Insurance Form Using Model-based Switching
We illustrate the use of property relevant with an example of an insurance application form. This form collects personal information in order to prepare an appropriate insurance policy and is divided into different sections. Not all sections of the form are relevant to every individual; the user's answer to specific questions is used to determine the relevance of each section.
As an example, if the user responds yes to the question
the user might be asked a series of questions pertaining to prior smoking- related health conditions. We first define a partial data model for this insurance application in Figure 5.4.
Figure 5.4 Property relevant makes portions of the data instance irrelevant.
< model xmlns ="http://www.w3.org/2002/xforms" id ="i1" schema ="insurance.xsd"> < instance > < person xmlns =""> < diet >...</ diet > < habits > < smoker >false</ smoker >... </ habits > < health >< chest />< lungs />< heart /></ health > </ person > </ instance > < bind nodeset ="/person/health"> < bind nodeset ="chest" relevant = "boolean-from-string(../habits/smoker)"/> < bind nodeset ="lungs" relevant = "boolean-from-string(../habits/smoker)"/> </ bind > </ model >
Next, we bind a user interface to this model in Figure 5.5. In this example, the user interface pertaining to the user's smoking-related health conditions are initially hidden because of the default value provided in the instance for field smoker . A value of false is used in the expression
Figure 5.5 Property relevant enables model-based conditional user interfaces.
< group xmlns ="http://www.w3.org/2002/xforms" ref ="/person"> < input ref ="habits/smoker"> < label >Do you smoke?</ label ></ input > < group ref ="health/chest"> < label >Chest Conditions</ label >...</ group > < group ref ="health/lungs"> < label >Lung Conditions</ label >...</ group > </ group >
relevant = "boolean-from-string(../habits/smoker)"
to set model property relevant on fields chest and lungs to false .
If the user changes this initial state by answering yes to the question
the relevant property applied to the nodes corresponding to chest and lungs becomes true . As a consequence, user interface controls that bind to the subtree that collects details about smoking-related health conditions are displayed to the user.