Playing with XML Namespaces in Mule 4 Dataweave

 Today, the world is full of REST APIs which use JSON as a major message type. But we have a lot of applications and APIs in the middleware world which rely on XML message structures. XML messages are a bit more complex than JSON messages, as in XML messages namespaces play a vital role. A miss in the namespace can lead to errors that are sometimes unseen to normal eyes. In this blog, we will discuss various ways by which we can handle the transformation of XML messages with or without namespaces in Mule 4 using Dataweave.

  1. Transforming XML messages without namespaces
  2. Transforming XML messages with namespaces 

1. Transforming XML messages without namespaces

This conversion is pretty simple, all we need to consider is XPath and do the mapping accordingly. This is similar to the JSON data transformation Let's take an example of 2 XMLs, wherein both input and output XMLs don't have namespaces. The output we need to specify is application/xml.



Suppose we want to extract the value of country from the above XML message. All we need to do is traverse to the node as we do in the case of JSON transformations i.e. (Assuming data is in the payload variable) . Below is the dataweave file for the transformation transforming the data to a different XML format.


%dw 2.0
output application/xml 
        Name: ++,
        Address :{
            Street: ++ , 



Above is the output for the dataweave file.

2. Transforming XML messages with namespaces.

Namespaces are really important for various systems to understand the element and its data. As the same element name with a different namespace corresponds to 2 different meanings. For example element name in an XML message with a namespace corresponding to the account namespaceis different from the element name corresponding to the contact namespace. So the business meaning of an element differs and so does the representation. An element belonging to a particular namespace will have a target namespace denoted by xmlns attribute. In some cases, namespaces are declared as attributes of elements and are referenced as prefixes like:
<acc:name xmlns:acc=""> or 
<cont:name xmlns:cont="">.

Let's take the below example:


<InputRoot xmlns="">
    <cont:contact xmlns:cont="">
        <cont:address xmlns:add="">

Now, in the above case, we have multiple namespaces. To deal with such scenarios we need to declare namespaces in the Dataweave file.

We can declare the namespace in the following way.
%dw 2.0
output application/xml
ns ns0
ns cont
ns add

In dataweave, an element with namespace will be denoted by ns#Element (ns denotes namespace prefix as declared, Element is the name of node or element of XML and, # acts as separator) 

Suppose we need to extract data of the country, now dataweave expression for the same will be "payload.ns0#InputRoot.cont#contact.cont#address.add#country"

Combining the above parameters, below is a sample dataweave file that will output the XML message generated from the above sample input XML, having different namespaces.


%dw 2.0
output application/xml
ns ns0
ns cont
ns add
ns ns1
ns cont1
ns add1
        cont1#Name: payload.ns0#InputRoot.cont#contact.cont#firstName ++ payload.ns0#InputRoot.cont#contact.cont#lastName,
        add1#Address :{
            add1#Street: payload.ns0#InputRoot.cont#contact.cont#address.add#street1 ++ payload.ns0#InputRoot.cont#contact.cont#address.add#street2 , 
            add1#City: payload.ns0#InputRoot.cont#contact.cont#address.add#city,

Below is the output for the above dataweave.


<ns1:OutputRoot xmlns:ns1="">
  <cont1:Contact xmlns:cont1="">
    <add1:Address xmlns:add1="">



Please share your valuable feedback 😊😊😊


Popular posts from this blog

DateTime formatting using xp20:format-dateTime ()

Handling XML response with or without namespace in Postman

Create Delimited String from XML Nodes and Vice Versa in SOA 12c