I want to access previous value element BaseChargePerUnit in the next iteration. The NewPrice value must be same in the next iteration as previous iteration's PreviousPrices value
%dw 1.0
%input payload application/xml
%output application/xml
---
using (w=payload.Order.OrderLines.*OrderLine default [], a=payload.*Order.PaymentMethods.*PaymentMethod default [])
{((payload.*Order default []) map {
POSLog:{
Transaction #(CancelFlag:"false"):{
((w default []) map {
LineItem #(EntryMethod:"Keyed"):{
Associate:{
CustomerOrderForDelivery:{
((w.LineCharges.*LineCharge default []) map {
RetailPriceModifier #(MethodCode:"AutomaticPromotion",VoidFlag:"false"):{
SequenceNumber:$$ + 1,
//Amount #(Action:"Subtract"):$.Extn.#BaseChargeAmount,
PreviousPrices:sessionVars.abc when $$ == 0 otherwise w[0].Extn.#BaseUnitPrice - $.Extn.#BaseChargePerUnit, // Clarify
NewPrice:w[0].Extn.#BaseUnitPrice - $.Extn.#BaseChargePerUnit
}
})
}}}})}}})}
This is sample output as
<?xml version='1.0' encoding='UTF-8'?>
<POSLog>
<Transaction CancelFlag="false">
<LineItem EntryMethod="Keyed">
<Associate>
<CustomerOrderForDelivery>
<RetailPriceModifier MethodCode="AutomaticPromotion" VoidFlag="false">
<SequenceNumber>1</SequenceNumber>
<PreviousPrices>10</PreviousPrices>
<NewPrice>8</NewPrice>
</RetailPriceModifier>
<RetailPriceModifier MethodCode="AutomaticPromotion" VoidFlag="false">
<SequenceNumber>2</SequenceNumber>
<PreviousPrices>8</PreviousPrices>
<NewPrice>7</NewPrice>
</RetailPriceModifier>
</CustomerOrderForDelivery>
</Associate>
</LineItem>
</Transaction>
</POSLog>
Input file is like this:
<?xml version="1.0" encoding="UTF-8"?>
<Order AuthorizationExpirationDate="2015-12-11T20:45:34-05:00">
<OrderLines>
<OrderLine AllocationDate="2015-12-11T00:00:00-05:00">
<Extn BaseLineTotal="3230.00" BaseListPrice="223.00" BaseUnitPrice="10" LineTotal="2000.00" POSDepartmentID="0623" />
<LineCharges>
<LineCharge ChargeAmount="40.00" ChargeCategory="SystemPromotion">
<Extn BaseChargeAmount="2" BaseChargePerLine="20.33" BaseChargePerUnit="2"/>
</LineCharge>
<LineCharge ChargeAmount="40.00" ChargeCategory="SystemPromotion" ChargeName="ProductDollarOffPromotion">
<Extn BaseChargeAmount="9.33" BaseChargePerLine="20.33" BaseChargePerUnit="1"/>
</LineCharge>
</LineCharges>
</OrderLine>
</OrderLines>
</Order>
Related
I have a Java payload in Mule 4.3 which contains an ArrayList:
agentList = {ArrayList}
0 = {SomeClass}
id = "0"
name = "Agent0"
1 = {SomeClass}
id = "1"
name = "Agent1"
I want to transform this to XML as:
<agentList>
<agent>
<id>0</id>
<name>Agent0</name>
</agent>
<agent>
<id>1</id>
<name>Agent1</name>
</agent>
</agentList>
If I do a DataWeave transform output application/xml --- result: payload, I get this XML:
<result>
<agentList>
<id>0</id>
<name>Agent0</name>
</agentList>
<agentList>
<id>1</id>
<name>Agent1</name>
</agentList>
</result>
How can I transform the ArrayList so that I get each item enclosed by agent tags and the entire list as agentList?
%dw 2.0
output application/xml
---
result: {
agentList: payload.*agentList map (value) -> { agent: value }
}
I have response coming like this -
<rsp stat="ok" version="1.0">
<result>
<tagObject>
<id>xx</id>
<tag_id>001</tag_id>
<type>Test</type>
<object_id>101</object_id>
<created_at>2013-10-09 11:41:45</created_at>
</tagObject>
<tagObject>
<id>xy</id>
<tag_id>002</tag_id>
<type>Test</type>
<object_id>102</object_id>
<created_at>2013-10-09 11:41:45</created_at>
</tagObject>
<tagObject>
<id>xz</id>
<tag_id>003</tag_id>
<type>Test</type>
<object_id>103</object_id>
<created_at>2013-10-09 11:43:44</created_at>
</tagObject>
</result>
</rsp>
Now I have to create a payload out of it, so that I can insert only <tag_id> and <object_id> into a database table.
Any suggestion on this? I was trying to work with Dataweave with Xpath3 but it did not work for me.
enter image description hereYou can convert Xml to Java map using dataweave
%dw 1.0
%output application/java
---
payload.rsp.result.*tagObject map ((tagObject , indexOfTagObject) -> {
tag_id: tagObject.tag_id as :string,
object_id: tagObject.object_id as :string
})
Out put: [{tag_id=001, object_id=101}, {tag_id=002, object_id=102}, {tag_id=003, object_id=103}].
This is my resultset from DB
{result={name=Sanjay,address=india,phone=xxxxxxxxxx}, {name=Amit,address=india,phone=xxxxxxxxxx}}
I want to transform as below format(xml format) with the help of transform message in mule-
<?xml version="1.0" encoding="UTF-8"?>
<resultset>
<lines>
<line1>
<name>Sanjay</name>
<address>india</address>
<phone>xxxxxxxxxx</phone>
</line1>
<line2>
<name>Amit</name>
<address>india</address>
<phone>xxxxxxxxxx</phone>
</line2>
</lines>
</resultset>
What I have done in transform message ,is like :
{
resultset: {
lines :{(payload.result map
"line$$" :{
name:$.name,
address:$.address,
phone:$.phone
}
)}
}
}
For this transformation I am getting
<resultset>
<lines>
<line0>
<name>Sanjay</name>
<address>india</address>
<phone>xxxxxxxxxx</phone>
</line0>
<line1>
<name>Amit</name>
<address>india</address>
<phone>xxxxxxxxxx</phone>
</line1>
</lines>
How to solve this problem?
Thanks in Advance
The following seems to work:
{
resultset: {
lines :{(payload.result map using (label = "line" ++ ($$ + 1))
(label) : {
name: $.name,
address: $.address,
phone: $.phone
}
)}
}
}
Output:
<?xml version='1.0' encoding='UTF-8'?>
<resultset>
<lines>
<line1>
<name>Sanjay</name>
<address>india</address>
<phone>xxxxxxxxx</phone>
</line1>
<line2>
<name>Amit</name>
<address>india</address>
<phone>xxxxxxxxxx</phone>
</line2>
</lines>
</resultset>
This alternative also gives the same result
%dw 1.0
%output application/xml
---
{
resultset: {
lines : {
(payload.result map {
("line" ++ ($$ + 1)) : {
name: $.name,
address: $.address,
phone: $.phone
}
})
}
}
}
Wrap the line structure inside the curly bracket map { ... }, and populate the key directly ("line" ++ ($$ + 1))
In Mule $$ points to current index value and just like C language this index starts from 0 so $$+1 is the only way to go about this.
I'm getting struggle in looping the entries in data weaver. Below is the Input and the expected response.
Not sure how to make loop(I need to get RecordEntry and each entry with 'IndividualEntry') .
Input xml : Record entry tag in input xml is 3, but I might get many. So need to make a loop as dynamic.
<?xml version="1.0" encoding="UTF-8"?>
<Records>
<storenumber />
<calculated>false</calculated>
<subTotal>12</subTotal>
<RecordsEntries>
<RecordEntry>
<deliverycharge>30.0</deliverycharge>
<entryNumber>8</entryNumber>
<Value>true</Value>
</RecordEntry>
<RecordEntry>
<deliverycharge>20.0</deliverycharge>
<entryNumber>7</entryNumber>
<Value>false</Value>
</RecordEntry>
<RecordEntry>
<deliverycharge>1.0</deliverycharge>
<entryNumber>6</entryNumber>
<Value>false</Value>
</RecordEntry>
</RecordsEntries>
</Records>
Expected Response ( I'm expecting the below response)
<?xml version="1.0" encoding="UTF-8"?>
<orders>
<order>
<StoreID />
<Total>false</Total>
<IndividualEntry>
<Number>8</Number>
<DeliverCharge>30.0</DeliverCharge>
</IndividualEntry>
<IndividualEntry>
<Number>7</Number>
<DeliverCharge>20.0</DeliverCharge>
</IndividualEntry>
<IndividualEntry>
<Number>6</Number>
<DeliverCharge>1.0</DeliverCharge>
</IndividualEntry>
</order>
</orders>
My Data weaver Transformation as below
%dw 1.0
%output application/xml
---
{
orders: {
order: {
StoreID:payload.Records.storenumber,
Total: payload.Records.calculated,
IndividualEntry: payload.Records.RecordsEntries.*RecordEntry map {
Number:$.entryNumber,
DeliverCharge:$.deliverycharge
}
}
}
}
Currently I'm getting response as below ( I don't know how to make each Record entry as a IndividualEntry tag, and also here element tag is added in extra which is not required in my case)
<?xml version="1.0" encoding="UTF-8"?>
<orders>
<order>
<StoreID />
<Total>false</Total>
<IndividualEntry>
<element>
<Number>8</Number>
<DeliverCharge>30.0</DeliverCharge>
</element>
<element>
<Number>7</Number>
<DeliverCharge>20.0</DeliverCharge>
</element>
<element>
<Number>6</Number>
<DeliverCharge>1.0</DeliverCharge>
</element>
</IndividualEntry>
</order>
</orders>
Could any one help me in fix this. Thanks in advance.
One way to do it:
orders: {
order: {
StoreID: payload.Records.storenumber,
Total: payload.Records.calculated,
(payload.Records.RecordsEntries.*RecordEntry map {
IndividualEntry: {
Number:$.entryNumber,
DeliverCharge:$.deliverycharge
}
})
}
}
Inside an object when you put an expression between parenthesis that returns an array of key-value pairs it is evaluated and used to fill the object.
See section5.1.3. Dynamic elements in https://developer.mulesoft.com/docs/dataweave
I'm new to Mule, please guide me how to insert new tag inside the XML structure using Mule Expression Language (MEL).Need to insert B tag in the below XML structure
<Test>
<A>table 1</A>
<C>table 3</C>
</Test>
Thanks in Advance.
My dom4j fu is limited but here is what I came up with:
<mulexml:xml-to-dom-transformer returnClass="org.dom4j.Document" />
<expression-component><![CDATA[
bNode = message.payload.rootElement.addElement('B');
bNode.text = 'table 2';
message.payload.rootElement.elements().add(1, bNode.detach());
]]></expression-component>
<mulexml:dom-to-xml-transformer />
This works fine with Mule 3.4.0.
Use a Data Weave component as below:
%dw 1.0
%output application/xml
%var myValue='MyValue'
%var B=''
---
myoutput:{
data: payload.Test ++ B:myValue
}