Multi-Output IF Statement

Here is what i am trying to acheive, i want an IF statement to give a result based on a matrix type.

if(((${roof_damage}= “4-6” or ${roof_damage}= “7-10”) and (${wall_damage}= “4-6” or ${wall_damage}= “7-10”)), “RED”, “BLUE”)

This is what my logic looks like, any suggestions

This looks like a legitimate if() expression. What specifically is your question?

BTW, if you want to do something like this:

if () then … else if () then … else …

it looks like:

if(${roof_damage}=“4-6”, “RED”, if(${roof_damage}=“7-10”, “BLUE”, "GREEN"))

Can this be applied to the rating type?

Where ${roof_damage} and ${wall_damage} are the questions and “4-6” can be replaced by variable names for each rating?

Not sure if I’m making any sense

The Kobo ‘rating’ widget is really just a select1 underneath.

These if() statements can be used in any XPath expression; that is, in any relevent, constraint or calculation, irregardless of the question type. The condition of the if() statement can be any valid boolean XPath expression, eg you could compare ${foo}=1, or {foo}=${bar}, or whatever. So yes, you could compare the result of, say, ${roof_damage} to the literal string “4-6”, or you could compare it to the value of some other question, say ${some_damage}. eg

if (${roof_damage}=${some_damage}, "RED", "BLUE")

Hi,
allow me please, a short question on if (). Can the TRUE or the ELSE syntactically be empty, like:.

  • if( condition, , ELSE)? (i.e. no true action specified)
  • if (condition, TRUE, )? (i.e. no else action specified).
    Greetings.

To be clear, in Xpath, if() is a function, not a statement. And, unlike a statement, a function is required to always return a value, so there is no notion of “no action”. So you really need to be explicit as to what you want this function to return if the condition evaluates to true, and what to return when false.

More precisely, in the relevant source code https://github.com/opendatakit/javarosa/blob/master/src/org/javarosa/xpath/expr/XPathFuncExpr.java:

public Object eval (DataInstance model, EvaluationContext evalContext) {
...
        if (name.equals("if")) {
            assertArgsCount(name, args, 3);
            return ifThenElse(model, evalContext, args, argVals);

That is, javaRosa will throw an runtime logic error if you dont provide 3 arguments. Whether your XPath parser throws a syntax error when attempting to parse the expression "if(true(), ,'foo')" is probably somewhat implementation dependent. But either way, you’ll be screwed eventually… :wink:

1 Like

Thank you very much!
I really like your answers with deeper/backup info.
Esp. the code link here was fully satisfying.
Best regards