Contact Us 1-800-596-4880

Insert Attributes to an XML Tag

DataWeave 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

This DataWeave example uses @(key:value) syntax (for example, @(loc: "US")) to inject attributes to XML tags. The example populates the title element with two attributes and each of the author elements with one attribute. Before you begin, note that DataWeave version 2 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave 1.0 (%dw 1.0) examples, within the Mule 3.9 documentation set. For other Mule versions, you can use the Mule Runtime version selector in the table of contents.

It uses these functions:

  • mapObject to iterate over each of the books in the input XML, defined in the variable myInput.

  • map to iterate over the author elements in each book.

DataWeave
%dw 2.0
var myInput = read('<bookstore>
  <book>
    <title>Everyday Italian</title>
    <year>2005</year>
    <price>30</price>
    <author>Giada De Laurentiis</author>
  </book>
  <book>
    <title>Harry Potter</title>
    <year>2005</year>
    <price>29.99</price>
    <author>J K. Rowling</author>
  </book>
  <book>
    <title>XQuery Kick Start</title>
    <year>2003</year>
    <price>49.99</price>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
  </book>
  <book>
    <title>Learning XML</title>
    <year>2003</year>
    <price>39.95</price>
    <author>Erik T. Ray</author>
  </book>
</bookstore>', 'application/xml')
output application/xml
---
bookstore: myInput.bookstore mapObject (book) -> {
      book : {
      title @(lang: "en", year: book.year): book.title,
      price: book.price,
      (book.*author map
      author @(loc: "US"): $)
    }
}
Output XML
<?xml version='1.0' encoding='UTF-8'?>
<bookstore>
  <book>
    <title lang="en" year="2005">Everyday Italian</title>
    <price>30</price>
    <author loc="US">Giada De Laurentiis</author>
  </book>
  <book>
    <title lang="en" year="2005">Harry Potter</title>
    <price>29.99</price>
    <author loc="US">J K. Rowling</author>
  </book>
  <book>
    <title lang="en" year="2003">XQuery Kick Start</title>
    <price>49.99</price>
    <author loc="US">James McGovern</author>
    <author loc="US">Per Bothner</author>
    <author loc="US">Kurt Cagle</author>
    <author loc="US">James Linn</author>
    <author loc="US">Vaidyanathan Nagarajan</author>
  </book>
  <book>
    <title lang="en" year="2003">Learning XML</title>
    <price>39.95</price>
    <author loc="US">Erik T. Ray</author>
  </book>
</bookstore>