97008 48403
+91 40-27177600

Blog

How to manage dataweave xml namespace declarations in Mule ESB?

single-blog

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about managing DataWeave XML namespace declarations in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

Assume we have the following scenario in which we have dataweave script that produces XML like

%dw 1.0

%output application/XML skipNullOn=”everywhere”

%namespace soap http://www.w3.org/2003/05/soap-envelope

%namespace ns http://www.mycompany/2015/07

{

soap#Envelope: {

soap#Header: {

},

soap#Body: {

ns#GetVehDetails: {

}

}

}

}

}

 

It produces something like this…

<soap:Envelope xmlns:soap=”http://www.w3.org/2003/05/soap-envelope”>

<soap:Header/>

<soap:Body>

<ns:GetVehDetails xmlns:ns=”http://www.mycompany/2015/07″>

The requirement is to change the dataweave script to output the xml namespace declarations at the root element mentioned below:

<soap:Envelope xmlns:soap=”http://www.w3.org/2003/05/soap-envelope”

xmlns:ns=”http://www.mycompany/2015/07″>

<soap:Header/>

<soap:Body>

<ns:GetVehDetails>

Our Solutions team took the following approach while working on this scenario real time.

STEP 1: Creating a new project.

In The Any point studio, top left clicks on the file => New => Mule Project. Click on the Mule Project enter your project name and click finish.

STEP 2:

Drag and drop the connectors required from the Mule Palette to the canvas as shown

STEP 3:

The requirement in the above scenario is a known issue and the only way we at Massil Technologies could solve it is listed below:

You need to add a dummy attribute @(ns#name:”) with the namespace that you want to be at top of the topmost root tag such as Envelope tag in this case

%dw 1.0

%output application/XML skipNullOn=”everywhere”

%namespace soap http://www.w3.org/2003/05/soap-envelope

%namespace ns http://www.mycompany/2015/07

{

soap#Envelope @(ns#name:””): {

soap#Header: {

},

soap#Body: {

ns#GetVehDetails: {

}

}

}

}

FLOW:

CONSOLE OUTPUT:

XML FILE: <?xml version=“1.0” encoding=“UTF-8”?>

XML FILE: <?xml version=“1.0” encoding=“UTF-8”?>

<mule xmlns:tracking=“http://www.mulesoft.org/schema/mule/ee/tracking” xmlns:file=“http://www.mulesoft.org/schema/mule/file” xmlns:dw=“http://www.mulesoft.org/schema/mule/ee/dw” xmlns=“http://www.mulesoft.org/schema/mule/core” xmlns:doc=“http://www.mulesoft.org/schema/mule/documentation”

xmlns:spring=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd

http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd

http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd

http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd

http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd”>

<file:connector name=“File” outputPattern=“C:\Users\MI\Desktop/out” autoDelete=“true” streaming=“true” validateConnections=“true” doc:name=“File”/>

<flow name=“datawave”>

<file:inbound-endpoint path=“src/main/resources/output” responseTimeout=“10000” doc:name=“File”/>

<byte-array-to-string-transformer doc:name=“Byte Array to String”/>

<logger message=“#[payload]” level=“INFO” doc:name=“Logger”/>

<dw:transform-message doc:name=“Transform Message” mode=“deferred”>

<dw:set-payload>

<![CDATA[

%dw 1.0

%output application/xml skipNullOn=”everywhere”

%namespace soap http://www.w3.org/2003/05/soap-envelope

%namespace ns http://www.mycompany/2015/07

{

soap#Envelope @(ns#name:””): {

soap#Header: {

},

soap#Body: {

ns#GetVehDetails: {

}

}

}

}

]]>

</dw:set-payload>

</dw:transform-message>

<byte-array-to-string-transformer doc:name=“Byte Array to String”/>

<logger message=“#[payload]” level=“INFO” doc:name=“Logger”/>

</flow>

</mule>

 

Hence, we have gone through the step by step process to depict the way Massil Technologies team has addressed the scenario mentioned above. Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Get in touch with us

Scroll to top