Reputation: 356
I want to write following nested if conditions in XQuery,
if(condition-1) {
if(nested-condition-11) {
....
}
if(nested-condition-12) {
....
}
}
else if(condition-2) {
if(nested-condition-21) {
....
}
if(nested-condition-22) {
....
}
}
else if(condition-3) {
if(nested-condition-31) {
....
}
if(nested-condition-32) {
....
}
}
else {
}
I have tried following code with XQuery,
if (condition-1) then
if(nested-condition-11) then
...
else ()
if(nested-condition-12) then
...
else ()
else if (condition-2) then
if(nested-condition-21) then
...
else ()
if(nested-condition-22) then
...
else ()
else if (condition-3) then
if(nested-condition-31) then
...
else ()
if(nested-condition-32) then
...
else ()
else()
But this isn't working. It's throwing following error,
Multiple markers at this line - line 310, column 9: Invalid expression: unexpected token: if - 2 changed lines
Kindly share some pointers on this. Thanks.
Upvotes: 0
Views: 6280
Reputation: 52878
I think the issue is that the then-expression and the else-expression have to be single expressions.
So what you have now kinda looks like this (not actual XQuery code):
if (condition) then
if statement <-- first expression
if statement <-- second expression
else
()
What you need to do is wrap the expressions in parentheses and separate them with a comma. Basically create a sequence...
if (condition) then
(if statement, if statement) <-- one expression
else
()
This is what your example would end up looking like (extra line breaks added for readability):
if (condition-1) then
(
if (nested-condition-11) then
'...'
else (),
if(nested-condition-12) then
'...'
else ()
)
else if (condition-2) then
(
if (nested-condition-21) then
'...'
else (),
if(nested-condition-22) then
'...'
else ()
)
else if (condition-3) then
(
if (nested-condition-31) then
'...'
else (),
if (nested-condition-32) then
'...'
else ()
)
else ()
Upvotes: 2
Reputation: 606
Here is a more contrived Production example which hits home:
declare namespace xf = "http://me.com/suspend/";
declare function xf:is-value-in-sequence ($value as xdt:anyAtomicType? , $seq as xdt:anyAtomicType* ) as xs:boolean
{
$value = $seq
} ;
declare function xf:checkBusinessRule($d1 as element(*), $d2 as element(*)) as element(*)
{
let $list := <results> {
for $rule at $pos in $d1//*:getBusinessCriteriaOutput
return
<temp pos="{$pos}">
<rule_id>{$rule/*:SUSPEND_RULE_ID/text()}</rule_id>
<op>{$rule/*:OPERATOR/text()}</op>
<val>{$rule/*:FIELD_VALUE/text()}</val>
</temp>
}
</results>
return <final>
{
for $a in $list//temp, $b in $d2//val
where $a/@pos = $b/@pos
return
if ($a/op = '=' and not($b/node())) then
<rec>
<rule_id>{data($a/rule_id)}</rule_id>
</rec>
else if ($a/op = '=' and fn:compare($a/val/text(), $b/text()) != 0) then
<rec>
<rule_id>{data($a/rule_id)}</rule_id>
</rec>
else if ($a/op ='LIKE' and not(fn:contains($b/text(), fn:replace($a/val/text(), '%', '')))) then
<rec>
<rule_id>{data($a/rule_id)}</rule_id>
</rec>
else if ($a/op='IN' and not(xf:is-value-in-sequence($b/text(), fn:tokenize($a/val/text(), '[,\s]+')))) then
<rec>
<rule_id>{data($a/rule_id)}</rule_id>
</rec>
else if ($a/op='NULL' and ($b/text() != '')) then
<rec>
<rule_id>{data($a/rule_id)}</rule_id>
</rec>
else if ($a/op='NOT NULL' and ($b/text() = '')) then
<rec>
<rule_id>{data($a/rule_id)}</rule_id>
</rec>
else ()
}
</final>
} ;
declare variable $d1 as element(*) external;
declare variable $d2 as element(*) external;
xf:checkBusinessRule($d1, $d2)
Upvotes: 0