0

我正在创建 xquery 中节点操作的替换值XQuery

但似乎这段代码不起作用,因此该IF-ELSE语句总是会出错。

这是我的代码:

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

我尝试使用此示例 XML,但结果始终不是XXX当字段revision具有值时。(这总是要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>

当前结果:

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

预期结果

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

对此有什么想法吗?

更新

正如下面 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 ()
         )

但似乎结果是一样的。它仍然进入 else () 语句。有任何想法吗?

之前谢谢你。

4

1 回答 1

1

问题是,empty()检查空序列,因此包含一个空字符串的序列trueempty(). 您可以将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 ()
于 2016-06-02T03:56:06.597 回答