Count choices in filtered option list of cascade

I have a cascading select for locations and sub-locations. Not all locations have a sub-location and if that is the case, I want to use relevant to hide the sub-location question. Is there a way to do this?

I tried what is described in the post linked below, but it seems to always count the total number of sub-location options, not the length of the filtered list.
count_filtered_cascade_options.xlsx (16.2 KB)

Your count just needs to be a tiny bit more specific and include a filter to only count those that match location.

count(instance('sub_locations')/root/item[${location}=location_filter])

count_filtered_cascade_options_edit.xlsx (16.4 KB)

2 Likes

BTW, now that all choice lists in XLSForms are converted to nodesets (see https://github.com/XLSForm/pyxform/pull/614) there is no longer any need for circa 2019 'tricks' to accomplish this. :tada:

So as @ahblake most astutely states, you just need to perform a little XPath 'black magic' to include the equivalent cascading select filter into your count() input nodeset. Yes it is a bit obscure, but there is tremendous (hidden) power in these XPath expressions that let you accomplish magical things like this! :magic_wand:

2 Likes

Terrific. Thanks both! Much appreciated.

Do I need to modify something for this to work inside of a repeat group? And will it work on a a list that's part of a select_one_from_file question?

Yes!

Your question suggests that it's not working as expected. :grimacing: The intent would be for no modification to be needed but it's possible there's an issue there because it is more complex. Can you show an example of what's not working?

EDIT: I was mistakenly referring to the question name and not the external file inside the instance('...') part of it. This file shows what wasn't working, it has my mistake: counting_options_length.xlsx (14.5 KB)

1 Like

I'm so glad you got it working!

I can absolutely see doing that too. Nothing at all along the way gave you any kind of warning or error??

It just didn't output anything. No errors or warnings when uploading the draft.

1 Like