Iteratively defining questions within a repeat group

1. What is the issue? Please be detailed.
Hello all,
I'm working on a survey where respondents will be asked for all of the water sources that they use in the form of a select_multiple question. Once they've selected all of the options that they use, I have a begin_repeat block that repeats based off of the number of choices selected (which I've defined as count-selected(${change_sources})). Within this repeat block, I'd like for each question to include the name of each type of source. However, I can't find a way to iterate through the space-delimited array returned by change_sources in order to have the questions change for each repetition. I've tried creating a counter that would be passed to the selected-at() function but can't find a way to reliably increment or decrement this.

I'm unable to publicly share the questions, but any insight would be greatly appreciated.

Hello Chinmay,

I assume the value you get for no. of selected options by using count-selected is stored in calculate variable tot_sel.

  • In the begin_repeat row for the desired block of questions, put tot_sel in the repeat_count.
  • Before all other questions in your repeat block, calculate the index of the loop iteration in calculate variable indx using position(..) function inside the loop.
  • Then create a calculate variable option_code for fetching the selected choice corresponding to the loop iteration by putting following function in calculate column. selected-at(${change_sources},indx-1)
  • In your question labels text you can use ${option_code} to display the option code as required.

Welcome @ChinmayT,
You can use the selected-at() funtion, see https://docs.getodk.org/form-operators-functions/#selected-at and https://getodk.github.io/xforms-spec/#fn:selected-at.

With the search function of this forum you can find further hints and examples, please, e.g. https://forum.getodk.org/search?q=selected-at%20select_multiple%20get%20items%20selected.

Hello Joy,

Thanks for the thorough instructions. I've implemented this as you described above in my form and it works well to give me the name of the choice. However, I'd like to be able to use the corresponding label for each name in my repeat group question.

I've read about the jr:choice-name() function, and I've tried implementing it as follows:
jr:choice-name(${option_code}, '${option_code}')

I've set this up within a calculate row and defined the result as src_label. However, when I use ${src_label} in the next question, it just returns a single space.

Do you have any tips on how to fix this or other ways that I could access the label?

Thanks,
Chinmay

Where/how do you want to use the label ${option_code} ?

I'm planning to use the label to fill in a question within the repeat block. For example, if ${src_name} is the label associated with ${option_code}, I would like to ask a question similar to:

How satisfied are you with the quality of ${src_name}?

I'm unable to use the jr:choice-name function to return ${src_name}

If no. of options is small in your choice list then you can calculate label based on option_code by using a nested if() statement in calculate column.
e.g.
if(${option_code}=1,'SourceA',if(${option_code}=2,'SourceB',''))

You can also use jr:choice-name function.
e g.
jr:choice-name(${option_code},'${change_sources}')

If no. is large then create a small CSV data file with same option list , so that you can use pulldata function to fetch the label based on the option_code.

Can you share the related extract of your XLSForm please? (Normally, you should not need to duplicate labels into an external file to reference labels.)

Based on my experience with form designing and analysis of the emerging dataset, I would advise, you avoid Repeats unless it is extremely neccesary. Just form a block/group of questions for each option of the multiple select question. Then use relevant to skip the entire group/block which the selection wasnt picked. This will save you from dynamic labels you want to create.

1 Like

Using flat groups instead of repeats may have disadvantages, e.g. for aggregate functions, unknown/dynamic number of "repeats" and (external) analysis. I would recommend to use the structure / data model which corresponds to the content logically.

I completely agree. Using "repeat" causes a lot of loading and crashing problems, especially when there are "nested repeats" in the form. Avoiding "repeat" might seem like the easy way out, but in my experience, it's worth it.

Hi @Joseph_Muganga
We are aware that there might be some bugs related to repeats. We have fixed some of them recently but there are some left. If you have experienced a crash please share your form (you can do that in private) so that we can reproduce it.

1 Like

Hi @Grzesiek2010,

Thank you so much for the information. I appreciate it and will share the form.