basex - XQuery Replace Value With Conditional Failing -
i in middle of creating xquery replace-value of node action in xquery
.
but seems code not working therefore if-else
statement going else.
this code:
declare function local:replacing($contextdata element()) element()* { copy $pipeline := $contextdata/handler/data/* modify( if(not(empty(data($pipeline/payload/sample/transactiontype)))) replace value of node $pipeline/payload/sample/transactiontype 'xxx' else (), if(not(empty(data($pipeline/payload/sample/revision)))) replace value of node $pipeline/payload/sample/revision 'xxx' else () ) return $pipeline };
i try against sample xml result not xxx
when field revision
having value. (this goes 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 this?
update
as recommended har below, change code into:
if(not(empty(data($pipeline/payload/sample/transactiontype)))) replace value of node $pipeline/payload/sample/transactiontype 'xxx' else ( if(not(empty(data($pipeline/payload/sample/revision)))) replace value of node $pipeline/payload/sample/revision 'xxx' else () )
but seems result same. still goes else () statement. ideas?
thank before.
the problem was, empty()
checks empty sequence, sequence containing 1 empty string considered true
empty()
. can pass data()
result if
since empty has effective boolean value of false
:
if(data($pipeline/payload/sample/transactiontype)) replace value of node $pipeline/payload/sample/transactiontype 'xxx' else (), if(data($pipeline/payload/sample/revision)) replace value of node $pipeline/payload/sample/revision 'xxx' else ()