【问题标题】:Nested If issue in XQueryXQuery 中的嵌套 If 问题
【发布时间】:2015-07-11 14:39:36
【问题描述】:

我想在 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 行更改

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

【问题讨论】:

    标签: oracle xquery soa osb nested-if


    【解决方案1】:

    我认为问题在于then-expression and the else-expression 必须是single expressions

    所以你现在拥有的看起来像这样(不是实际的 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 ()  
    

    【讨论】:

      【解决方案2】:

      这是一个更人为的生产示例:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 2018-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多