---
schema_version: "1"
id: b8d503854494845ffcf88a5b4c3662e098555c3818e3a375d494a6848902b25a
title: Insert Attributes to an XML Tag
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.3/dataweave-cookbook-insert-attribute.md
canonical_url: https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-insert-attribute
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 30e210c73390841a98f18369b24dc427bc4ac2312422a4a70cd1fc70228bae6e
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: DataWeave
version: 2.3
keywords: studio, anypoint, transform, transformer, format, xml, json, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Insert Attributes to an XML Tag

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

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.

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 Script:

```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
<?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>
```

## Related Examples

- [Output Self-closing XML tags](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-output-self-closing-xml-tags)

- [Remove Specified XML Attributes](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-remove-certain-xml-attributes)

- [Include XML Namespaces](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-include-xml-namespaces)

## See Also

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.3/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook)
