Constraints in ODK

Validate the form before sending to aggregate.

@Abdullah_Khan

it must be something wrong in your form. You can try my sample form and see that it works well
Probably you use

in the wrong way. You can atach your xls form and I'll tel you.

Try this for your Constraint (in ODK Build):

. <= /data/HH_sourceIncome/HH_MnthlyIncome

[ODK Build will take care of properly escaping the '<=' for you, so dont do anything special here]

Specifically, if you export the XML you should get something like this in your bindings:

  ...
  <bind nodeset="/data/HH_sourceIncome/HH_MnthlyIncome" type="int"/>
  <bind nodeset="/data/HH_sourceIncome/HH_MnthlyAgriIncome" type="int" constraint="(. &lt;= /data/HH_sourceIncome/HH_MnthlyIncome)"/>
  ...

The critical bit is that the pathname (to your HH_MnthlyIncome element) in the constraint expression must match the nodeset pathname (ie above it) of the element. [this pathname may vary depending on what group hierarchy the question(s) in your form are tucked under...]

Yes, as mentioned in the above error message there was a bad node '()' in some questions. I found this '()' in 'relevant' head of the XLS Form under some questions, when i converted ODK File into XLS.

What does '()' denote in 'relevant' head??

As I did not enter them in the ODK File. By removing these from the XLS Form I can now convert the XLS Form to XML file.

Great. Thanks Gareth. This '. <= /data/HH_sourceIncome/HH_MnthlyIncome' seemed to work.

Hey Gareth looking at the code you shared how will be use this code if we want to put a constraint using SUM of 2 responses. Like for example if we extend the above constraint and want the response to the HH agri income should be less than HH monthly income + HH other source of income??

I tried '. <= /data/HH_sourceIncome/HH_MnthlyIncome+HH_OtherIncome' but obviously I am wrong.

You are very close! Basically, in raw XML XForm, you (normally) have to specify the full pathname to any elements used in XPath expressions, which your Constraint calculation in reality is. So it should probably be something more like:

. <= (/data/HH_sourceIncome/HH_MnthlyIncome + /data/HH_sourceIncome/HH_OtherIncome)

That is, both your operands - HH_MnthlyIncome and HH_OtherIncome - need their full pathname.

BTW - I prefer to include the parenthesis too, just to make it clear that you aren't assuming the '+' operator has a higher precedence than the '<=' operator [I can never remember, so I like to play it safe].

Oh, because you are now pulling into multiple values into your calculations, unless all of the inputs (ie both HH_MnthlyIncome and HH_OtherIncome) are required/mandatory, you can run the risk of falling victim to 'The Curse of NaN', which can lead to confusing results (follow link for easy solution... :wink: ]

Thanks Mate for the assistance.

Hi Xiphware, few months back you mentioned me a separate path way to be used in ODK Build and it worked. I just wanted to know how do we give path way in ODK Build in case of using 'Relavance' option.

I want a group of questions to be shown once a response on a specific question in another group is 'No'.

Like, my first question is 'whether the respondent was contacted?' - Group name=Inter_Detail, Question name=First_Available, Type=Choose One, Options: Yes and No

If we enter 'Yes' then proceed with subsequent questions, but if 'No' then ask group of questions like:
What was the reason of non-availability?
When was second contact made?
Was the respondent available on second contact?

I tried entering the following codes in the relevance option in group head
${First_Available} = 'No',
/data/Inter_Detail/First_Available = 'No',

but these dont seem to work. Any suggestions??

Your logic appears to be on the right track: ie put all the dependent questions in a group and add a relevant expression on the group. And your relevant="${First_Available} = 'No'" looks about right. But could you perhaps post the actual form to be sure; maybe there's a typo somewhere... BTW are you still using ODK Build? Or XLSForm?

Using ODK Build still. So i think full pathways will be required...no?
I have attached the XLSForm exported from ODK Build for the form Im working on ODK Build.

Survey-export.xlsx (11.9 KB)

Hi, I am working on a form over odk build which I need to make question 10 to be relevant when any of the first 3 options in question 9 is select.

I suspect the problem is that your relevant expression is checking against 'no'

/data/Inter_Detail/First_Available = 'no'

...but your First_Available select_one question has the option values "Yes" or "No". XPath string comparisons using '=' are strictly case-sensitive. D'oh! :wink:

[and yes, Build wants full pathnames, not ${First_Available} XLSForm shortcuts]

ohh man :face_with_hand_over_mouth::shushing_face:.. perfect it worked :stuck_out_tongue:. Thanks once again!!!