0
votes

desiring an output like this

</b>
 <c>
            <value1 />
</c>  
 <c>
            <value2 />
</c>  
 <c>
            <value3/>
</c>

instead of this

<a>
    </b>
     <c>
                <value1 />
    </c>  
     <c>
                <value2 />
    </c>  
     <c>
                <value3/>
    </c>
</a>

via dataweave. i understand the output wont necessarily be completely valid xml

any help appreciated thanks

1

1 Answers

1
votes

Because it is going to miss a single root element it is not going to be valid XML. So DataWeave is not going to be able to read or write it directly. You can generate an XML for each element and concatenate them as a string to avoid having an XML parsing error.

The input is not valid, I fixed it as this:

<a>
   <b/>
   <c>
      <value1 />
   </c>  
   <c>
      <value2 />
    </c>  
   <c>
      <value3/>
   </c> 
</a>

Script:

%dw 2.0
output application/java
---
(payload[0] pluck (value,key,index) -> {(key):value} )
    reduce ((item, acc="") -> acc ++ write(item, "application/xml",  {"writeDeclaration":false}) ++ "\n" ) 

Output:

<b/>
<c>
  <value1/>
</c>
<c>
  <value2/>
</c>
<c>
  <value3/>
</c>

Note that I added a newline at the end of each string ("\n") for clarity. You can remove it if you want.