Need Help With XPath Expression

This is what the expression currently says for weighted_malnutrition: if(#form/question2/RISC_Score_Calculation/z-score_table_look_up_standard_deviation = -3, 6, if(#form/question2/RISC_Score_Calculation/z-score_table_look_up_standard_deviation = -2, 3, 0))

This is what we are trying to say (Refer to attached table for reference of parameters): if(#form/question2/RISC_Score_Calculation/z-score_table_look_up_standard_deviation < -3, 6, if(#form/question2/RISC_Score_Calculation/z-score_table_look_up_standard_deviation > = -3 and #form/question2/RISC_Score_Calculation/z-score_table_look_up_standard_deviation < -2, 3, if(#form/question2/RISC_Score_Calculation/z-score_table_look_up_standard_deviation > -2, 0, 0)))

I am not sure if what is described in the table can be translated to an XPath Expression. If so, would the above stated expression be correct?

Hi Monique, yes, that looks like it should work. Those expressions are evaluated in sequence, so you don't need to duplicate conditions, that is, the first condition establishes that anything after it is not less than -3, so there's no need to check that it's greater than or equal to -3.

Also, you can also use a cond expression, which should simplify the calculations a little. That would look like this:

cond(

#form/question2/RISC_Score_Calculation/z-score_table_look_up_standard_deviation < -3, 6,

#form/question2/RISC_Score_Calculation/z-score_table_look_up_standard_deviation < -2, 3,

Thanks Evan! That makes a more sense. I will definitely switch to a condition expression.

I'm not sure if you know much about z-score calculations and Lookup Tables, but following the CommCare Help guide (Calculate a Z-Score in a Form - CommCare Public - CommCare Public) I created the hidden value to calculate the approximate z-score from the table. This corresponds to the child being more than -3, -2 and -1 standard deviations from the mean. Would I need to add in lines within this expression to correspond with a child that is more than -4 or -5 standard deviation from the mean? Or will the application automatically calculate this?

(i.e. if(/data/weight < instance('zscores')/zscore_list/zscore[gender = /data/gender][month = /data/age]/sd4neg, -4, if(/data/weight < instance('zscores')/zscore_list/zscore[gender = /data/gender][month = /data/age]/sd3neg, -3, if(/data/weight < instance('zscores')/zscore_list/zscore[gender = /data/gender][month = /data/age]/sd2neg, -2, if(/data/weight < instance('zscores')/zscore_list/zscore[gender = /data/gender][month = /data/age]/sd1neg, -1,0))))

Hi Monique, you should be okay as long as you don't need to distinguish between -3, -4, and -5. It looks like the z-score calculation on that page will call anything less than 3 standard deviations below the mean "-3" (even if it's actually 4 or 5 std deviations below).

if(/data/weight < instance('zscores')/zscore_list/zscore[gender = /data/gender][month = /data/age]/sd3neg, -3, if(/data/weight < instance('zscores')/zscore_list/zscore[gender = /data/gender][month = /data/age]/sd2neg, -2, if(/data/weight < instance('zscores')/zscore_list/zscore[gender = /data/gender][month = /data/age]/sd1neg, -1,0)))

However, if that is the expression you're using, then I don't think the conditionals we've discussed would work, since everything below 3 std deviations is called "-3", nothing meets the "< -3" condition. This expression only returns 4 possible values, -3, -2, -1, or 0. You can modify the conditional to check for those exactly, using "=" rather than "<".

You could also work with those conditional expressions directly - I believe this would do what you're looking for:

cond(

/data/weight < instance('zscores')/zscore_list/zscore[gender = /data/gender][month = /data/age]/sd3neg, 6,

/data/weight < instance('zscores')/zscore_list/zscore[gender = /data/gender][month = /data/age]/sd2neg, 3,

That is, call anything below 3 standard deviations "6", anything below 2 standard deviations "3", and anything else "0".