0

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.

randytan
  • 1,029
  • 5
  • 23
  • 52

1 Answers1

1

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 ()
randytan
  • 1,029
  • 5
  • 23
  • 52
har07
  • 88,338
  • 12
  • 84
  • 137
  • thanks for your update. But seems that this IF-Else logic still getting me the same result. Only else block is return. I made changes to first `replace value of node` by removing else () since it is giving me an error in the baseX. Any ideas? I will update the question. – randytan Jun 02 '16 at 04:04
  • Can you post sample XML that should go to `if` block? Your current XML rightly go to `else` since it has empty `transactionType`, while the `if` evaluates to `true` when `transactionType` is not empty, right? – har07 Jun 02 '16 at 04:12
  • Hi har, thanks for your comments. Seems the logic which i need to update is not one by another. It should be parallel. So in my expected result it is: `if the value is none`, then show as `none`. Else `if there is a value` just show `'XXX'`. Is it possible to update two of field simultaneously? – randytan Jun 02 '16 at 04:15
  • I see, I completely misunderstood the problem. In this case, your initial XQuery is closer to solution than mine. The problem was, `empty()` checks for empty sequence, so sequence containing one empty string is considered `true` by `empty()` – har07 Jun 02 '16 at 04:33
  • wow, thanks for your generous help. I already try your solution. But seems still getting the same result, Har. The `` still goes to `else()` statement, in addition if i put a value inside `` it is also executed the `else()` part. Seems this xquery cannot change it. i use baseX 8.3 by the way. – randytan Jun 02 '16 at 04:40