How to populate multiple tags in an if / else using XQuery?

0

I want to do something like:

for $var in $myValueOfVar
    return
        if ($var= "X") then
            <tagA>A</tagA>
            <tagB>B</tagB>
            <tagC>C</tagC>
        else
            <tagD>D</tagD>
}

But I get the following error:

  

Invalid expression, expecting ELSE, found '>'

So I tried to use 4 if 's, one for each tag, as below:

for $var in $myValueOfVar
    return
        if ($var= "X") then
            <tagA>A</tagA>
        else
            ()
        if ($var= "X") then
            <tagB>B</tagB>
        else
            ()
        if ($var= "X") then
            <tagC>C</tagC>
        else
            ()
        if ($var != "X") then
            <tagD>D</tagD>
        else
            ()
}

But I get the following error:

  

Invalid expression: unexpected token: if

What is the correct way to do this? I found no example following this line.

    
asked by anonymous 29.10.2018 / 14:16

1 answer

0

After some time researching, I had an answer in the StackOverflow community (but in gringa), the solution follows below, if someone comes across the same scenario:

for $var in $myValueOfVar
    return
        if ($var= "X") then (
            <tagA>A</tagA>,
            <tagB>B</tagB>,
            <tagC>C</tagC>,
        )
        else
            <tagD>D</tagD>
}
    
29.10.2018 / 21:05