CommCare – How to Filter Instances by Case Property

commcare

I am working on the CommCare mLabour app, which tracks women from the onset of labour through delivery via digital partograph. For the contractions graph, I need to write an instance which filters to cases whose contraction intensity is 'low'. In the app, this case property is contractions_dur = 'low'. I tried the following path, and am getting an xpath error in the app:

instance('casedb')/casedb/case[@case_type='Indicators'][index/parent=current()/@case_id][@status='open']/[contractions_dur = 'low']

The error message says invalid xpath 'expected beginning of path'

The only CommCare Help Site page I can find about instance syntax is this one about CommCare session instance paths. What is the proper syntax for an instance filtering for a specific case with a certain case property value?

Best Answer

If you're filtering cases, you want an expression that ultimately returns a set of cases. The first part of the expression, instance('casedb')/casedb/case, gives you all cases on the phone.

The expressions in square brackets then filter the cases by case type, parent (you're limiting to cases that are children of the "current" case, which is the one you selected from the case list), and open/closed.

[@case_type='Indicators']
[index/parent=current()/@case_id]
[@status='open']

The /[contractions_dur = 'low'] is what's throwing the error. The / says to go another level deep, but I suspect what you want is to just add the contractions_dur as a fourth constraint, on the same level as the others. So try dropping the slash: instance('casedb')/casedb/case[@case_type='Indicators'][index/parent=current()/@case_id][@status='open'][contractions_dur = 'low']