xquerybasexxquery-update

XQuery Replace Value With Conditional Failing


i am in the middle of creating xquery replace-value of node action in XQuery.

But seems this code is not working therefore the IF-ELSE statement is always going to 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. (this always goes to else statement)

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:

 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. Any ideas?

Thank you before.


Solution

  • The problem was, empty() checks for empty sequence, so sequence containing one empty string is considered true by empty(). You can just pass data() result to if since empty has Effective Boolean Value of 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 ()