How to fill multiple tags in an if/Else using Xquery?

Asked

Viewed 34 times

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 mistake:

Invalid Expression, expecting ELSE, found '>'

So I tried using 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 right way to do this? I haven’t found any example following this line.

1 answer

0


After some time researching, I got a response in the Stackoverflow community itself (but in the 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>
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.