0
votes

Assuming I have XMLs like

    <a>
     <b>Some Value</b>
    </a>

    ...or...
    <a>
     <b type=1 />
    </a>

    ...or... 
    <a/>

and want to have some values and attributes defined in output like

    <a>
     <b type=0>Some Value</b>
    </a>

    ...or...
    <a>
     <b type=1>Empty</b>
    </a>

    ...or... 
     <a>
     <b type=0>Empty</b>
    </a>

what would be best way to do so in Mulesoft?

Using script with lines like

if (payload.a == null ) payload.a={}
if (payload['a']['b']) payload['a']['b']={}
if (payload.a.b.type == null) payload.a.b.type=0;

or dataweave

%dw 1.0
%output application/xml
---
{
   a: payload.a default { {b:{ b@type=0 }} }
}

I'm confused about syntax here.

1

1 Answers

2
votes

If I understand correctly what you're asking, the following seems to work:

input:

<?xml version='1.0' encoding='UTF-8'?>
<root>
   <a>
     <b>Some Value</b>
    </a>
    <a>
     <b type="1" />
    </a>
    <a/>
</root>

Dataweave:

%dw 1.0
%output application/xml
---
root: payload.root.*a mapObject (
    a: {
        b @(type: $.b.@type default "0"): 
            $.b when $.b != null and $.b != "" otherwise "Empty"
    }
)

output:

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <a>
    <b type="0">Some Value</b>
  </a>
  <a>
    <b type="1">Empty</b>
  </a>
  <a>
    <b type="0">Empty</b>
  </a>
</root>