Skip to main content

Decipher Support

All the topics, resources needed for Decipher.


FocusVision Knowledge Base

Condition Tag: Create Simpler References to Condition Logic

1:  Overview

The <condition> element enables you to declare a named condition. Instead of rewriting condition logic across your entire survey, you can use the <condition> element to write it once and then reference the condition using the element's label.

For example:

<radio label="Q1" optional="0">
  <title>Are you...</title>
  <row label="r1">Male</row>
  <row label="r2">Female</row>

<condition label="male" cond="Q1.r1">Is Male</condition>
<condition label="female" cond="Q1.r2">Is Female</condition>

<block label="Male_Section" cond="condition.male">

<block label="Female_Section" cond="condition.female">

In the code above, specifying condition.male is the same as specifying Q1.r1. This means that the "Male_Section" <block> will only be shown to those who are condition.male, or Q1.r1.

The <condition> element may be added anywhere within the survey's XML, but it's best practice to write them at the top of the file (just below the <samplesources> element).

2:  Attributes

There are two attributes available for <condition> elements:

Attribute Type Description
cond string The condition's condition logic. This should be a Python expression that will evaluate to True or False.
label string The condition's name. This is what you will use to reference the condition's logic.

If needed, you can also add builder:warning="unsupported" to flag an element as unsupported and prevent a project from launching.

2.1:  cond - Set the Condition

The cond attribute is the logic for the <condition> element. Instead of rewriting the same condition logic across a survey, you can specify the logic inside the cond attribute of a <condition> element and refer to it instead.

For example:

<number label="Q2" size="3" verify="range(1, 125)">
  <title>Please enter your age below:</title>

<condition label="isAdult" cond="Q2.ival ge 18">Age is 18 or older</condition>

<radio label="Q3" optional="0" cond="condition.isAdult">
  <title>Have you ever been to a casino before?</title>
  <row label="r1">Yes</row>
  <row label="r2">No</row>

<radio label="Q4" optiona="0" cond="condition.isAdult">
  <title>Have you ever been to a bar before?</title>
  <row label="r1">Yes</row>
  <row label="r2">No</row>

Instead of writing the condition Q2.ival ge 18 for both questions Q3 and Q4, we declared a <condition> element named "isAdult" and wrote the condition inside of the cond attribute. With this <condition> element in place, we can refer to the logic using the condition.label syntax (e.g. condition.isAdult) and show both questions Q3 and Q4 appropriately.

2.2:  label - Specify the Condition's Name

The label attribute is the <condition> element's name. It's best to set this attribute to a human readable string that can be easily remembered and typed out.

For example:

<condition label="maleTV" cond="Q1.r1 and Q2.r3 and (Q3.r1 or Q3.r2 or Q3.r3)">Males who frequently watch TV</condition>

<condition label="maleGamers" cond="Q1.r1 and (Q2.r4 or Q2.r5)">Males who game via console or PC</condition>

<condition label="femaleTV" cond="Q1.r2 and Q2.r3 and (Q3.r1 or Q3.r2 or Q3.r3)">Females who frequently watch TV</condition>

<condition label="femaleGamers" cond="Q1.r2 and (Q2.r4 or Q2.r5)">Females who game via console or PC</condition>

3:  Create a Conditions Table

There is a special mutator function available to automatically create a table in the report containing a row for every <condition> element in a survey.

Add the following to your survey's XML:

<label onLoad="createConditionTable()" />

This will automatically generate a table named "conditions" in the report. For example:

<checkbox label="Q1" atleast="1" optional="0">
  <title>Please select all that apply:</title>
  <row label="r1">Item 1</row>
  <row label="r2">Item 2</row>
  <row label="r3">Item 3</row>
  <row label="r4">Item 4</row>

<condition label="oneItem" cond="Q1.count == 1">1 item selected at Q1</condition>
<condition label="twoItems" cond="Q1.count == 2">2 items selected at Q1</condition>
<condition label="threePlusItems" cond="Q1.count gt 2">3+ items selected at Q1</condition>

<label onLoad="createConditionTable()" />

The code above automatically generates the following table in the report:


This information will also be available in the data downloads. Because this creates a new variable, moving, deleting or adding new <condition> elements will require an hmerge.

4:  What's Next?

Learn more about creating sections with the Block Tag.