Skip to content
ENG

Multiple selector

Given the following input payload:

<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>

Write a DataWeave transformation that produces the following JSON output:

{
"authors": [
"Gambardella, Matthew",
"Ralls, Kim",
"Corets, Eva"
]
}
  • With xml, where tags tend to repeat, we can use the multiple selector .* to get those repeated tags at the same level.
  • The multiple selector will always return an Array.

🔒🔓Show solution
%dw 2.0
output application/json
---
{
authors: payload.catalog.*book.author
}

Notes:

  1. Since book is a tag that repeats several times, we can use payload.catalog.*book to get every book. If we used just payload.catalog.book we would get the first book only.
  2. Since payload.catalog.*book returns an Array of objects, we can simply append .author, which returns every value whose key is Author, building the new Array we are after.
{Code mat;}

Learn MuleSoft: courses, exercises and quizzes. Free, no sign-up.