Complex choice filter

Hello Team and how are you doing?

I am working on a script on the Excel form. I have like four questions - q1 is a select one, q2, is a multiple response, and q3, it is a single response.

So, I want to filter the selected in q1, q2, and q3. I got the right logic for multiple. I used this for multiple responses- selected(${q2}, name). And it works well

Now, I want the answer selected in q1 and q3 to display. Please how do I go about this.

I will appreciate your input. Thanks,

It is a little hard for me to follow your description. Are you able to share your XLSForm for what you've tried so far?

Hi Danbjoseph, I really appreciate your feedback. I attached the xls form to see the issue.

q1: is an awareness
qb: Options not picked in q1a will be displayed here
q1c: both options selected in q1a and q1b will not display.
qd: All options selected in q1a, q1b, and q1c should display here. This is where I have a challenge.

Kindly help me out.


Try using this as the choice-filter for q1d:

selected(${q1b}, name) or selected(${q1a}, name) or selected(${q1c}, name) 

Hello danjoseph, Many thanks for your feedback. I really appreciate it.

I have tried the logic, and it works perfectly. Thank you so much.

But I noticed for q1g it was not working well. For q1g, I want the remaining options that were not selected in q1d to display in q1g. Please, how do I go about that?

If the below screenshots are what you want, try this as the choice-filter for q1g:

( selected(${q1b}, name) or selected(${q1a}, name) or selected(${q1c}, name) ) and not( selected(${q1d},name) )

Just an observation, but for select_one questions using a selected() or contains() to check the result is a bit of overkill; a simple equality test should suffice because a select_one will only ever contain a single value [sic], or nothing. ie

${q1a}=name , ${q1c}=name

Please note, contains() is really meant specifically for substring matches; again, no harm done here, but it is surplus to need (and potentially dangerous). Also, applying these XPath functions during expression evaluation will take computationally longer than a simple equality test [only milliseconds to be sure, but hey, life is short :wink: ]

Warning: dont use contains() for checking select_multiple responses. It can give a false positive match if one of your option values inadvertently happens to be a substring of another; eg "apple" vs "pineapple", or even '2' vs '12'. Instead always use selected() to check select_multiple responses.


Thanks @Xiphware, I started with what they were using and didn't realize that issue with it.

@bashirmuheeb, I've edited my answers to change the contains() to selected() to account for the warning above. For optimization you can further adjust the selected() to the simple equality test if it is checking the answer to a select_one. I'll admit, I didn't understand your choice of select_one type for q1a and q1c based on the wording of the questions.

1 Like

danbjoseph, many thanks for the comment. I have tried it and everything works well. Thank you so much. I really appreciate it

1 Like