0

我想在 XQuery 中编写以下嵌套 if 条件,

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 {        
}

我已经尝试使用 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()  

但这行不通。它抛出以下错误,

此行有多个标记 - 第 310 行,第 9 列:无效表达式:意外标记:if - 2 行更改

请分享一些关于此的指示。谢谢。

4

2 回答 2

2

我认为问题在于then-expression 和 else-expression必须是单个表达式

所以你现在拥有的看起来像这样(不是实际的 XQuery 代码):

if (condition) then
    if statement <-- first expression
    if statement <-- second expression
else
    ()

您需要做的是将表达式括在括号中并用逗号分隔它们。基本上创建一个序列...

if (condition) then
    (if statement, if statement) <-- one expression
else
    ()

这就是您的示例最终的样子(为便于阅读添加了额外的换行符):

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 ()  
于 2015-05-01T21:20:06.577 回答
0

这是一个更人为的生产示例,它很受欢迎:

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)
于 2015-05-04T20:04:15.727 回答