---
schema_version: "1"
id: 2c6e4e51bc9710536467398670d15abe421fc66cdcb76c2eef4bbaef6fc039ff
title: Remove Specified XML Attributes
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.4/dataweave-cookbook-remove-certain-xml-attributes.md
canonical_url: https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-remove-certain-xml-attributes
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 23053a3e6488a711357a6a1a901acd37d99fbf65053d5187e103e46aab9bc6eb
description: MuleSoft Documentation Site
release: "2.4"
latest: false
---
---
product: DataWeave
version: 2.4
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Remove Specified XML Attributes

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

You might want to remove specific attributes from within an XML tag that are known to contain
sensitive data. The DataWeave example defines a function in the DataWeave header that removes a
specific XML attribute from the passed element and its children.

This example uses:

- `mapObject` to go through each element.

- `@` to refer to the attributes in an XML element.

- `if` and `else` to conditionally only list XML attributes when they are present, and also to only
  recursively call the function when a child element is an object.

- `is` to check if each child is of type `Object` .

- `-` to remove an element from the list of XML attributes.

- `inlineCloseOn="empty"` on the output directive, so that the output XML displays self-closing tags
  when there are no values.

If the parent `users` object or child `user` objects within the input contain an attribute with the
key `password`, `removeAttributes` removes that attribute from the object.

DataWeave Script:

```dataweave
%dw 2.0
output application/xml inlineCloseOn="empty"
var removeAttribute = (element,attrName) ->
  element mapObject (value, key) -> {
    // Pass the name of the key of the input object,
    // for example, whether the key is "users" or "user",
    // and use @() to remap the XML attributes.
    (key) @(
        (
          // If the element contains attributes (key.@?),
          // remove any attribute that has the provided key,
          // "password".
          if (key.@?)
            (key.@ - attrName)
          // Otherwise, do nothing.
          else {}
        )
    ) :
    // If the value of the input object contains one or
    // more objects, apply removeAttribute to remove any
    // attribute with the key "password" from those objects.
    if (value is Object)
      removeAttribute(value, attrName)
    // Otherwise, return the value of the input object.
    else value
  }
---
removeAttribute(payload, "password")
```

Input XML Payload:

```xml
<users>
  <user username="Julian" password="1234"/>
  <user username="Mariano" password="4321"/>
</users>
```

Output XML:

```xml
<?xml version='1.0' encoding='US-ASCII'?>
<users>
  <user username="Julian"/>
  <user username="Mariano"/>
</users>
```

## Related Examples

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

- [Insert an Attribute into an XML Tag](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-insert-attribute)

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

- [Exclude Fields from the Output](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-exclude-field)

- [Output a Field When Present](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-output-a-field-when-present)

- [Conditionally Reduce a List through a Function](https://docs.mulesoft.com/dataweave/2.4/dataweave-cookbook-conditional-list-reduction-via-function)

## See Also

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

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