首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XQuery中嵌套的If问题

XQuery中嵌套的If问题
EN

Stack Overflow用户
提问于 2015-05-01 18:03:06
回答 2查看 5.4K关注 0票数 0

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

代码语言:javascript
复制
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遵循代码,

代码语言:javascript
复制
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更改行

请分享这方面的一些建议。谢谢。

EN

回答 2

Stack Overflow用户

发布于 2015-05-01 21:20:06

我认为问题是然后-表达式和其他表达式必须是单表达式

因此,您现在拥有的内容类似于这样(不是实际的XQuery代码):

代码语言:javascript
复制
if (condition) then
    if statement <-- first expression
    if statement <-- second expression
else
    ()

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

代码语言:javascript
复制
if (condition) then
    (if statement, if statement) <-- one expression
else
    ()

这就是您的示例的最终结果(为可读性添加了额外的换行符):

代码语言:javascript
复制
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 ()  
票数 2
EN

Stack Overflow用户

发布于 2015-05-04 20:04:15

下面是一个更有针对性的生产示例:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29992386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档