简体   繁体   English

XQuery用条件失败替换值

[英]XQuery Replace Value With Conditional Failing

i am in the middle of creating xquery replace-value of node action in XQuery . 我正在在XQuery中创建节点操作的xquery替换值。

But seems this code is not working therefore the IF-ELSE statement is always going to else. 但是似乎此代码无法正常工作,因此IF-ELSE语句总是可以执行其他操作。

This is my code: 这是我的代码:

declare function local:replacing($contextData as element()) 
as element()*
{    
    copy $pipeline := $contextData/Handler/Data/*

     modify(

   if(not(empty(data($pipeline/Payload/sample/transactionType)))) then 
     replace value of node $pipeline/Payload/sample/transactionType with 'XXX' else (),
   if(not(empty(data($pipeline/Payload/sample/revision)))) then
     replace value of node $pipeline/Payload/sample/revision with 'XXX' else ()

      )
 return $pipeline
 };

I try against this sample XML but the result is always not XXX when the field revision is having value. 我尝试使用此示例XML,但是当字段revision具有价值时,结果始终不是XXX (this always goes to else statement) (这总是转到else语句)

let $result :=
<root>
<Handler>
  <Data>
    <root>

      <Payload>
            <sample>
            <transactionType></transactionType>
            <revision>123</revision>
            <board>1</board>
            <mission>1</mission>
            <method>Manual</method>
            <listOfBoard>
              <board>
                <type>small</type>
                <amount>5054</amount>
                <token>300</token>
              </board>
            </listOfBoard>
            <pricing>300</pricing>
            <playing>Wed</playing>
          </sample>   
    </Payload>

    </root>

  </Data>
</Handler>
</root>

Current Result: 当前结果:

<root>
  <Payload>
    <sample>
      <transactionType/>
      <revision>123</revision>
      <board>1</board>
      <mission>1</mission>
      <method>Manual</method>
      <listOfBoard>
        <board>
          <type>small</type>
          <amount>5054</amount>
          <token>300</token>
        </board>
      </listOfBoard>
      <pricing>300</pricing>
      <playing>Wed</playing>
    </sample>
  </Payload>
</root>

Expected Result 预期结果

<root>
      <Payload>
        <sample>
          <transactionType/>
          <revision>XXX</revision>
          <board>1</board>
          <mission>1</mission>
          <method>Manual</method>
          <listOfBoard>
            <board>
              <type>small</type>
              <amount>5054</amount>
              <token>300</token>
            </board>
          </listOfBoard>
          <pricing>300</pricing>
          <playing>Wed</playing>
        </sample>
      </Payload>
    </root>

Any ideas for this? 有什么想法吗?

Update 更新资料

As recommended by Har below, i change the code into: 如下面Har所建议,我将代码更改为:

 if(not(empty(data($pipeline/Payload/sample/transactionType)))) then 
             replace value of node $pipeline/Payload/sample/transactionType with 'XXX'
         else (
            if(not(empty(data($pipeline/Payload/sample/revision)))) then
            replace value of node $pipeline/Payload/sample/revision with 'XXX' else ()
         )

But seems the result is the same. 但是似乎结果是一样的。 It still goes to else () statement. 它仍然转到else()语句。 Any ideas? 有任何想法吗?

Thank you before. 谢谢你

The problem was, empty() checks for empty sequence, so sequence containing one empty string is considered true by empty() . 问题在于, empty()检查空序列,因此empty()认为包含一个空字符串的序列为true You can just pass data() result to if since empty has Effective Boolean Value of false : 您可以将data()结果传递给if因为empty具有有效布尔值false

if(data($pipeline/Payload/sample/transactionType)) then 
    replace value of node $pipeline/Payload/sample/transactionType with 'XXX' else (),
if(data($pipeline/Payload/sample/revision)) then
    replace value of node $pipeline/Payload/sample/revision with 'XXX' else ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM