XForms also allows you to conditionally display part of a form based on some value in your instance data. Typically this is used when the answer to one field of the form conditionally displays another part.

The format of this is to use the xf:bind statement within in the xf:model.

In the following example, the second input field is conditionally displayed based on the integer value of the first instance value.

<htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:xf="http://www.w3.org/2002/xforms"xmlns:xs="http://www.w3.org/2001/XMLSchema"><title>Testing the XForms bind relevant attribute.</title><head><xf:model><xf:instance><varxmlns=""><first>1</first><second>This is the second value</second></var></xf:instance><xf:bindnodeset="/var/first"type="xs:decimal"/><xf:bindnodeset="/var/second"relevant="/var/first &gt; 0"/></xf:model></head><body><p>Demonstration of relevant fields.</p><xf:select1ref="/var/first"><xf:label>Should I show the second question? </xf:label><br/><xf:itemselect="yes"><xf:label>Yes Please!</xf:label><xf:value>1</xf:value></xf:item><xf:item><xf:label>No Thank You</xf:label><xf:value>0</xf:value></xf:item></xf:select1><br/><xf:inputref="/var/second"><xf:label>Second value: </xf:label></xf:input></body></html>

Sometimes you have a sequence of many items and the display of one item depends on the values of other items. Predicates are a way of appending AND/OR operations to the end of a path expression used as a relevancy expression.

The following example uses a bind with two predicates. The second item in a sequence is bound to the first item. To do this you must select the second item item[2] in the nodeset and add [. > 2.0] to end of the item[1] predicate.

<htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:xf="http://www.w3.org/2002/xforms"xmlns:xsd="http://www.w3.org/2001/XMLSchema"><head><xf:model><xf:instanceid="instanceData"><dataxmlns=""><item>0.00</item><item>0.00</item></data></xf:instance><!-- this rule will only allow the second item to be displayed if the first value is over 2.0 --><xf:bindnodeset="instance('instanceData')/item[2]"relevant="instance('instanceData')/item[1][. &gt; 2.0]"/></xf:model></head><body><xf:inputref="instance('instanceData')/item[1]"><xf:label>First item: </xf:label></xf:input><br/><xf:inputref="instance('instanceData')/item[2]"><xf:label>Second item: </xf:label></xf:input></body></html>

Since parts of a form are usually either visible or not visible, it is natural to use a boolean value to determine if the field should be displayed.

In this example if InputIndicator is true, the second output instance is visible.

If the InputIndicator is false, the second output is not visible.

Note that expression .='true' is used. This is a string comparison. Ideally you would just be able to test a node using mynoode=true() but I have had problems with this method.

<?xml version="1.0" encoding="UTF-8"?><htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:xf="http://www.w3.org/2002/xforms"xmlns:ev="http://www.w3.org/2001/xml-events"xmlns:xs="http://www.w3.org/2001/XMLSchema"><head><title>Example of binding to to boolean controls</title></head><body><xf:modelid="model"><xf:instanceid="input"><DataInxmlns=""><InputIndicator>false</InputIndicator></DataIn></xf:instance><!-- make the input data type be a XML Schema type boolean --><xf:bindid="input_bind"nodeset="/DataIn/InputIndicator"type="xs:boolean"></xf:bind><!-- second instance bound to outputs --><xf:instanceid="output"><DataOutxmlns=""><OutputValue>Hello World!</OutputValue></DataOut></xf:instance><!-- if the input is true, then the output is relevent --><xf:bindid="output_bind"nodeset="instance('output')/OutputValue"relevant="instance('input')/InputIndicator[.='true']"/></xf:model><p><xf:inputbind="input_bind"><xf:label>Check to see the value of output: </xf:label></xf:input><br/><xf:outputbind="output_bind"><xf:label>Value of Output: </xf:label></xf:output></p></body></html>

In this example, a simple checkbox is used to conditionally display a field.

<htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:xf="http://www.w3.org/2002/xforms"xmlns:xs="http://www.w3.org/2001/XMLSchema"><title>Testing the relevant attribute of the bind element with boolean values.</title><xf:model><xf:instance><varxmlns=""><first>true</first><second/></var></xf:instance><xf:bindnodeset="/var/first"type="xs:boolean"/><xf:bindnodeset="/var/second"relevant="/var/first='true'"/></xf:model><body><p>The input field should only display if the first value is checked.</p><xf:inputref="/var/first"><xf:label>Display the next input?: </xf:label></xf:input><br/><xf:inputref="/var/second"><xf:label>Display this only if the first answer is true: </xf:label></xf:input></body></html>

In this example the type cast to boolean does not have any impact since relevant="/var/first=true()" does not work as expected. String comparison must be used.

An Architecture for Conditional Views and User-Maintainable Rules[edit]

Sometimes you need to have a consistent way of conditionally displaying views in a form. For example, if you want non-programmers to maintain the business logic of when a view is displayed, the binding rules to a view can be generated by an external rules engine.

To make this work you need to create a central instance that is used to control form views:

We call this architecture one of "Named Views" because an external tool can be used to state the rules of how any named view is rendered. This will be a central location in the form that generates the view instance and the binding expressions.

Each view is wrapped in a group element that binds the group to the instance in the view:

<xf:groupref="instance('views')/named-view-1"><!-- these elements will be conditionally displayed based on the relevancy of named-view-1 --></xf:group><xf:groupref="instance('views')/named-view-2"><!-- these elements will be conditionally displayed based on the relevancy of named-view-2 --></xf:group><xf:groupref="instance('views')/named-view-2"><!-- these elements will be conditionally displayed based on the relevancy of named-view-3 --></xf:group>

The display rules for each view can then be stored in a bind statement:

You can then build another XForms application that allows non-programmers to maintain these rules files and and a simple transform that places the instance and bind statements in the XForms model when it is loaded. The original groups may still need to be manually added to the form but once the views are added the rules can be maintained with a separate application.