XPath Query Question

I have a lookup table multiselect question that looks up cases with the case_id as the value field.

I now want to have another field that uses the selected case_id's to get a list of tracking_numbers(another field in the cases) for the selected cases.

I tried the following:

  • Creating a hidden question, lets call it #form/query_question, to produce this text based cases selected **

@case_id = '866b9f5d-7e6b-4c92-a64d-73ee14194385' or @case_id = 'fa6e42c7-53fc-468f-b982-2762b34a02a1'

  • When I try put this in the following xpath query like the one below I get no results

join(" ", instance('casedb')/casedb/case[index/parent = instance('commcaresession')/session/data/case_id][#form/query_question]/tracking_number)

  • When I enter the query directly into the Xpath expression get results

join(" ", instance('casedb')/casedb/case[index/parent = instance('commcaresession')/session/data/case_id][@case_id = '866b9f5d-7e6b-4c92-a64d-73ee14194385' or @case_id = 'fa6e42c7-53fc-468f-b982-2762b34a02a1']/tracking_number)

Can anyone please help me know what I'm doing wrong or if theres a better way to achieve what i want?

I think the best way to accomplish what you are outlining is to keep the cases in the space separated format from the multi-select question and using the selected() function to check for set inclusion.

IE: If the question #form/multiselect was the result of selecting a list of cases, it would be set to something like:

866b9f5d-7e6b-4c92-a64d-73ee14194385 fa6e42c7-53fc-468f-b982-2762b34a02a1

In order to create a list of the tracking_number case properties for those cases, you could use a filter expression like

join(" ", instance('casedb')/casedb/case[selected(#form/multiselect, @case_id)]/tracking_number)

Your additional parent pre-filter would probably improve the runtime of that expression as well.

One important thing to note: the result of the casedb expression above will be sorted by the natural ordering of the case database, not by the ordering of the selected cases. This might naturally match up, since I think the mutliselect data source would also be sorted by the natural casedb ordering, but if you need it to be absolutely accurate you can use the sort-by function to create a guaranteed ordering based on a second list, like the following, which would sort the tracking numbers by the alphanumeric sorting of their uuid case_id value (you'd need to sort the resulting #form/multiselect as well so they would match up)

sort-by(
join(" ", instance('casedb')/casedb/case[selected(#form/multiselect, @case_id)]/tracking_number),
join(" ", instance('casedb')/casedb/case[selected(#form/multiselect, @case_id)]/@case_id)
)

Hope that helps

Hi Clayton,

This worked perfectly, you saved my bacon. Cheers.