%dw 2.0
output application/xml
---
books: {
(payload filter ($.properties.year > attributes.publishedAfter) map (item) -> {
book @(year: item.properties.year): {
(vars.exchangeRate.USD map {
price @(currency: $.currency): $.ratio * item.price
}),
title: item.properties.title,
authors: { (item.properties.author map {
author: $
}) }
}
} )
}
Reference Multiple Inputs
This DataWeave example takes three different input JSON files, one in the payload, another in a variable and the third in an attribute. All of them are parts of the same Mule Event. The payload contains an array of book objects, the variable has a set of currency exchange rates, and the attribute specifies a year to be used as a query. Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For DataWeave in Mule 3 apps, refer to DataWeave version 1.2 examples. For other DataWeave versions, you can use the version selector in the DataWeave table of contents.
The example uses the following:
-
filter
to leave out the books that are older than the date specified in the incoming attribute. -
map
to go through each book in the incoming array. Anothermap
function then goes through the currencies in the variable to calculates the book’s price in each one. A thirdmap
function lists out all authors of a book in case there are more than one. -
@
to define an XML attribute in the output.
[
{
"type": "book",
"price": 30,
"properties": {
"title": "Everyday Italian",
"author": [
"Giada De Laurentiis"
],
"year": 2005
}
},
{
"type": "book",
"price": 29.99,
"properties": {
"title": "Harry Potter",
"author": [
"J K. Rowling"
],
"year": 2005
}
},
{
"type": "book",
"price": 41.12,
"properties": {
"title": "Mule in Action",
"author": [
"David Dossot",
"John D'Emic"
],
"year": 2009
}
},
{
"type": "book",
"price": 49.99,
"properties": {
"title": "XQuery Kick Start",
"author": [
"James McGovern",
"Per Bothner",
"Kurt Cagle",
"James Linn",
"Kurt Cagle",
"Vaidyanathan Nagarajan"
],
"year": 2003
}
},
{
"type": "book",
"price": 39.95,
"properties": {
"title": "Learning XML",
"author": [
"Erik T. Ray"
],
"year": 2003
}
}
]
{
"publishedAfter": 2004
}
exchangeRate
:{
"USD": [
{"currency": "EUR", "ratio":0.92},
{"currency": "ARS", "ratio":8.76},
{"currency": "GBP", "ratio":0.66}
]
}
<?xml version='1.0' encoding='US-ASCII'?>
<books>
<book year="2005">
<price currency="EUR">27.60</price>
<price currency="ARS">262.80</price>
<price currency="GBP">19.80</price>
<title>Everyday Italian</title>
<authors>
<author>Giada De Laurentiis</author>
</authors>
</book>
<book year="2005">
<price currency="EUR">27.5908</price>
<price currency="ARS">262.7124</price>
<price currency="GBP">19.7934</price>
<title>Harry Potter</title>
<authors>
<author>J K. Rowling</author>
</authors>
</book>
<book year="2009">
<price currency="EUR">37.8304</price>
<price currency="ARS">360.2112</price>
<price currency="GBP">27.1392</price>
<title>Mule in Action</title>
<authors>
<author>David Dossot</author>
<author>John D'Emic</author>
</authors>
</book>
</books>