Nav

Example: Reference Multiple Inputs

This 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.

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. Another map function then goes through the currencies in the variable to calculates the book’s price in each one. A third map function lists out all authors of a book in case there are more than one.

  • @ to define an XML attribute in the output.

DataWeave

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%dw 2.0
output application/xml
---
books: {
  (payload filter ($.properties.year > attributes.publishedAfter) map  (item)   ->  {
    book @(year: item.properties.year): {
      (variables.exchangeRate.USD map {
        price @(currency: $.currency): $.ratio * item.price
      }),
      title: item.properties.title,
      authors: { (item.properties.author map {
        author: $
      }) }
    }
  } )
}
Input Payload

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[
  {
      "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
      }
  }
]
Input Attributes

         
      
1
2
3
{
  "publishedAfter": 2004
}
Input Variable exchangeRate

         
      
1
2
3
4
5
6
7
{
  "USD": [
    {"currency": "EUR", "ratio":0.92},
    {"currency": "ARS", "ratio":8.76},
    {"currency": "GBP", "ratio":0.66}
  ]
}
Output

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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>

In this topic: