Replace a string in Dataweave during XML to XML conversion - mule

My input XMl is
<?xml version="1.0" encoding="UTF-8"?>
<DEBMAS01>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<DIRECT>2</DIRECT>
<IDOCTYP>DEBMAS01</IDOCTYP>
<MESTYP>DEBMAS</MESTYP>
<SNDPOR>MULESOFT</SNDPOR>
<SNDPRT>LS</SNDPRT>
<SNDPRN>MULESOFT</SNDPRN>
<RCVPOR>MULESOFT</RCVPOR>
<RCVPRT>LS</RCVPRT>
<RCVPRN>MULESOFT</RCVPRN>
</EDI_DC40>
<E1KNA1M SEGMENT="1">
<MSGFN>005</MSGFN>
<KUNNR>0000099500</KUNNR>
<KTOKD>ZAG2</KTOKD>
<LAND1>SK</LAND1>
<NAME1>Mulesoft 99003 2nd</NAME1>
<SPRAS>E</SPRAS>
<SPRAS_ISO>EN</SPRAS_ISO>
</E1KNA1M>
My expected output XML is
<?xml version="1.0" encoding="UTF-8"?>
<DEBMAS01>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<DIRECT>2</DIRECT>
<IDOCTYP>DEBMAS01</IDOCTYP>
<MESTYP>DEBMAS</MESTYP>
<SNDPOR>PROJECT1</SNDPOR>
<SNDPRT>LS</SNDPRT>
<SNDPRN>MULESOFT</SNDPRN>
<RCVPOR>MULESOFT</RCVPOR>
<RCVPRT>LS</RCVPRT>
<RCVPRN>MULESOFT</RCVPRN>
</EDI_DC40>
<E1KNA1M SEGMENT="1">
<MSGFN>005</MSGFN>
<KUNNR>0000099500</KUNNR>
<KTOKD>ZAG2</KTOKD>
<LAND1>SK</LAND1>
<NAME1>Mulesoft 99003 2nd</NAME1>
<SPRAS>E</SPRAS>
<SPRAS_ISO>EN</SPRAS_ISO>
</E1KNA1M>
Here i just want to replace the string in SNDPOR tag from "MULESOFT" to "PROJECT1". I have achieved this using XSLT. But now i want to achieve this using Dataweave script. Kindly help.

Found the solution after going through the docs. Thanks to Rocco for enlightening me.
The solution is
%dw 1.0
%output application/xml
---
DEBMAS01 :{(payload map {
IDOC #(BEGIN:payload.DEBMAS01.IDOC.#BEGIN):{
EDI_DC40 #(SEGMENT:payload.DEBMAS01.IDOC.EDI_DC40.#SEGMENT):payload.DEBMAS01.IDOC.EDI_DC40 mapObject {
"$$" : $ unless "$$" ~="SNDPOR" otherwise "PROJECT1"
},
E1KNA1M #(SEGMENT:payload.DEBMAS01.IDOC.E1KNA1M.#SEGMENT):payload.DEBMAS01.IDOC.E1KNA1M mapObject {
"$$": $
}
}
})
}

Related

Update value after loading xml file

i am loading the following xml file using the readUrl command
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<a>
<b>xyz</b>
<c>
<d>false</d>
<e>false</e>
<f>true</f>
</c>
</a>
</soapenv:Body>
</soapenv:Envelope>
in the same dataweave as the readUrl command - i would like to update the xml e value from false to true
can someone please assist
cheers,
jonny
Using the update operator available in Mule 4.3.0 can be used to update the value as required:
%dw 2.0
output application/xml
ns soapenv http://schemas.xmlsoap.org/soap/envelope/
var soapMessage=readUrl("...","application/xml")
---
soapMessage update {
case .soapenv#Envelope.soapenv#Body.a.c.e -> true
}

Karate: XML - CDATA print not as an xml format

When I print response of the SOAP which has CDATA, it contains gt and lt as shown bellow. Can you please help to convert this to proper xml or can you help me to validate some of the value of the response.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:processRequestResponse xmlns:ns="http://controller.core.xxx.xxxxx.xxxx.xxxxxx.com">
<return><?xml version="1.0" encoding="UTF-8"?>
<xxx:xxxxxxxxxxxx xmlns:xxx="http://xxxxx.com/xxxx/xxxx/xxx/response/xyz/abc">
<Header><destinationId>ABC</destinationId>
..............
..............
...............
</Body>
</xxx:xxxxxxxxxxxxxResponse></return>
</ns:processRequestResponse>
</soapenv:Body>
</soapenv:Envelope>
I think one line will do the trick for you, try this:
* xml temp = $response//return

How to embed CDATA in Mulesoft

I'm trying to embed a literal CDATA value in a Mulesoft flow and cannot figure out how to do so.
My desired output (in an HTTP request body) is:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<query xmlns="http://autotask.net/ATWS/v1_5/">
<sXML>
<![CDATA[<queryxml><entity>ticket</entity><query><condition><field>id<expression op="equals">12345</expression></field></condition></query></queryxml>]]>
</sXML>
</query>
</soap:Body>
</soap:Envelope>
My Dataweave transformation looks as follows:
%dw 1.0
%output application/xml
%namespace soap http://schemas.xmlsoap.org/soap/envelope
---
{
soap#Envelope #(version: "1.0") : {
soap#Header: {},
soap#Body: {
query: {
sXML: "<queryxml><entity>ticket</entity><query><condition><field>id<expression op=\"equals\">12345</expression></field></condition></query></queryxml>"
}
}
}
}
But when I send this request to requestb.in (to inspect the contents), I can see it's coming through like this (focus on the sXML entity):
<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" version="1.0">
<soap:Header/>
<soap:Body>
<query>
<sXML><queryxml><entity>ticket</entity><query><condition><field>id<expression op="equals">12345</expression></field></condition></query></queryxml></sXML>
</query>
</soap:Body>
</soap:Envelope>
How can I get a literal CDATA value in there via dataweave / MEL?
Thank you.
I would try:
sXML: "<queryxml> .... </queryxml>" as :cdata
See https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-formats#custom-types-2 for more information.
handleInteractiveOrder:
write(vars.subm.soap#Envelope.soap#Body.cp#handleInteractiveOrder,"application/xml")
as CData
in mule 4 we can do like this. I kept my XML in subm variable.

Mule ESB Transform Message xml conversion

I am facing one issue while xml format conversion in mule transform message.
I am having one input xml file.
and I have to convert the input xml to output xml where the node structure is different and node names are different.
I used data transform message for this conversion, But at one point, it is showing expected format is object and found string.
Can any one please help me for the same.
"Type mismatch
found :name, :string
required :name, :object (com.mulesoft.weave.mule.exception.WeaveExecutionException). Message payload is of type: WeaveMessageProcessor$WeaveOutputHandler"
input payload
<?xml version="1.0" encoding="utf-8"?>
<AGREEMENT>
<details>
<newTransaction>N</newTransaction>
<type>ddd</type>
</details>
</AGREEMENT>
output Payload
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<TestHeader xmlns="TestWebService">
<Username>aaa</Username>
<Password>aaa</Password>
</TestHeader>
</soap:Header>
<soap:Body>
<AGRMNT>
<testId>
<_-Test_Agrmnt- SEGMENT="1">
<transaction>N</transaction>
</__-Test_Agrmnt->
</testId>
</AGRMNT>
</soap:Body>
</soap:Envelope>
DataWeave code
%dw 1.0
%output application/xml
%namespace soap http://schemas.xmlsoap.org/soap/envelope/
{
soap#Envelope: {
soap#Body: {
AGREEMENT: {
testId: {
'_-Test_Agrmnt-': {
transaction: payload.AGREEMENT.details.newTransaction as :string
}
}
}
}}
I have got following output with your input and script
<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AGREEMENT>
<testId>
<_-Test_Agrmnt->
<transaction>N</transaction>
</_-Test_Agrmnt->
</testId>
</AGREEMENT>
</soap:Body>
</soap:Envelope>
The only change is adding --- in script
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/xml
%namespace soap http://schemas.xmlsoap.org/soap/envelope/
---
{
soap#Envelope: {
soap#Body: {
AGREEMENT: {
testId: {
'_-Test_Agrmnt-': {
transaction: payload.AGREEMENT.details.newTransaction as :string
}
}
}
}}}]]>
</dw:set-payload>
</dw:transform-message>

Output Encoding issue in mule esb

I have used to do create XML using "Transform Message" Node in Mule ESB.
%dw 1.0
%output application/xml
item: {
name: payload."profile_name",
id: payload."profile_Id"
}
This shown a preview of output xml as
<?xml version='1.0' encoding='windows-1252'?>
<item>
<name>
<profile_name>????</profile_name>
</name>
<id>
<profile_Id>1</profile_Id>
</id>
</item>
This resulted encoding as 'windows-1252' how can i change encoding as 'UTF-8' in my outpout xml ?
Go to your Anypoint Studio folder.
Edit AnypointStudio.ini
Add:
-Dfile.encoding=UTF-8
Restart
Easy, define encoding in the output line:
%dw 1.0
%output application/xml encoding="utf-8"
---
name: payload.name?
the result:
<?xml version='1.0' encoding='utf-8'?>
<name>false</name>
For dataweave in runtime above 3.8.4 I implemented this solution:
set output as variable:
Output FlowVar - fv_itemName_xml
inside of Transform Message:
'%output application/xml writeDeclaration=false
item: { name: payload."profile_name", id: payload."profile_Id" }
and output will be:
<item>
<name>
<profile_name>????</profile_name>
</name>
<id>
<profile_Id>1</profile_Id>
</id>
</item>
Then define a variable as below:
Name: fv_outputName_xml
Value: #[flowVars.fv_itemName_xml]
Encoding: ISO 10646/Unicode(UTF-8)
Mime type: application/xml
And you have better control of Encoding and Mime type.
In my case I was need to feed that into Stored Procedure and without this workarround constantly receiving error:
System.Data.SqlClient.SqlException (0x80131904): XML parsing: line 1,
character 39, unable to switch the encoding