---
schema_version: "1"
id: e05a2c228eada5172dd647855c8c65f63f08b32112449191091167562508d424
title: 指定した XML 属性の削除
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.6/dataweave-cookbook-remove-certain-xml-attributes.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-cookbook-remove-certain-xml-attributes
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 2827ea99c46cf9a1dde55842687e09edadd2c926dcc8fa92e9266bfa052f55ca
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
# 指定した XML 属性の削除

XML タグ内から機密データが含まれていることがわかっている特定の属性を削除できます。この DataWeave の例では、渡される要素とその子から特定の XML 属性を削除する関数を DataWeave ヘッダーで定義します。 開始する前に、Mule 4 アプリケーションでは DataWeave のバージョン 2.x が使用されることに注意してください。Mule 3 アプリケーションでの DataWeave については、​[DataWeave バージョン 1.2 の例](#1.2@dataweave::dataweave-examples.adoc)​を参照してください。 他の DataWeave バージョンの場合は、DataWeave の目次のバージョンセレクターを使用できます。

この例では、以下を使用します。

- 各要素を調べる ​`mapObject`​。
- XML 要素の属性を参照する ​`@`​。
- 存在している XML 属性のみをリストし、子要素がオブジェクトである場合にのみ関数を再帰的にコールする ​`if`​ および ​`else`​。
- 子が ​`Object`​ 型かどうかをチェックする ​`is`​。
- XML 属性のリストから要素を削除する ​`-`​。
- 値がない場合に出力 XML に自己終了タグが表示されるようにする出力ディレクティブの ​`inlineCloseOn="empty"`​。

入力内の親 ​`users`​ オブジェクトまたは子 ​`user`​ オブジェクトにキーが ​`password`​ の属性が含まれる場合、​`removeAttributes`​ によってオブジェクトからその属性が削除されます。

DataWeave スクリプト:

```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")
```

入力 XML ペイロード:

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

出力 XML:

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

## [](#関連する例)関連する例

- [自己終了 XML タグの出力](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-cookbook-output-self-closing-xml-tags)
- [XML タグへの属性の挿入](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-cookbook-insert-attribute)
- [XML 名前空間を含める](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-cookbook-include-xml-namespaces)
- [出力からの項目の除外](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-cookbook-exclude-field)
- [存在する項目の出力](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-cookbook-output-a-field-when-present)
- [関数を使用したリストの条件付き削減](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-cookbook-conditional-list-reduction-via-function)

## [](#関連情報)関連情報

- [DataWeave セレクター](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-selectors)
- [DataWeave の解説書](https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-cookbook)