XForms/Conditional Styling

Motivation
You have a list of items that you want to display inside of a repeat loop. You want some items to have different styling based on the content of items.

Method
We will use the XForms group element to conditionally display an item inside of the repeat statement.

Sample XML Fragement
Assume your form has the following data in an instance:

Conditional Display of Items
The following text will only output element2 when element1 has a value of true.

The syntax .[a='b'] says that from the current node if the element a equals the value b then output the elements within the group.

Conditional Formatting of Items
This same strategy can be used to enclose output in different div or span elements.

In the above example all nodes with element1='true' will have a class="strong" in the output and any element with element1='false' will not have the attribute in the output.

Sample XForms Appliction
Load XForms Application

Source Code
In this example we need to display a list of synonyms for a specific term. The Synonyms are all listed and a preferred term is displayed with a bold font.

  Synonym Set (preferred term is bold) Synonyms:         

Discussion
XForms 1.1 also includes the if attribute. Some XForms can conditionally use this for display of elements.