Search code examples
mulemule-studiomule-componentmule-eldataweave

How to declare namespace in response xml


I'm doing POC in Data Weaver. I'm not sure how to declare namespace ns0: in throughout the output xml. Referred link: https://developer.mulesoft.com/docs/dataweave (I have seen examples on handling input xml with namespace 1.4.3, but I'm looking for output xml).

Input Xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <Records>
      <storenumber>12</storenumber>
      <calculated>false</calculated>
      <subTotal>12</subTotal>
   <Records>

Expected Output xml:

<?xml version='1.0' eencoding="UTF-8" standalone="yes"?>
 <ns0:orders 
       xmlns="ns2=urn:test.com:common:v1">
  <ns0:order>
    <ns0:StoreID>12</ns0:StoreID>
    <ns0:Total>false</ns0:Total>
   </ns0:order>
  </ns0:orders>

DataWeaver Transformation

%dw 1.0
 %output application/xml
 %var baseurl="ns2=urn:test.com:common:v1"
 ---
 orders @(xmlns:baseurl): {
 order: {
  StoreID: payload.Records.storenumber,
  Total: payload.Records.calculated
  }
}

I believe @(xmlns:baseurl) in the transformation is correct. Is there any other way to define it?


Solution

  • Namespaces are declared with the directive %namespace <prefix> <uri>
    You can specify the namespace of an element with <prefix>#<element>

    DataWeave Transformation

    %dw 1.0
    %output application/xml
    %namespace ns0 urn:test.com:common:v1
    ---
    ns0#orders: {
      ns0#order: {
        ns0#StoreID: payload.Records.storenumber,
        ns0#Total: payload.Records.calculated
      }
    }
    

    Output

    <?xml version='1.0' encoding='UTF-8'?>
    <ns0:orders xmlns:ns0="urn:test.com:common:v1">
      <ns0:order>
        <ns0:StoreID>12</ns0:StoreID>
        <ns0:Total>false</ns0:Total>
      </ns0:order>
    </ns0:orders>