---
schema_version: "1"
id: c1799f84bc4a17743901352392ef80ce42d2b17326c3f86bcee5fee91e51da26
title: XML 名前空間を含める
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.6/dataweave-cookbook-include-xml-namespaces.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.6/dataweave-cookbook-include-xml-namespaces
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 9be4ffa71021a143ef855486321dbb66e384a4a96f3e41b5ed10c7dd67e8d5f1
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
# XML 名前空間を含める

ヘッダーでさまざまな名前空間を定義し、各タグで参照できます。 開始する前に、Mule 4 アプリケーションでは DataWeave のバージョン 2.x が使用されることに注意してください。Mule 3 アプリケーションでの DataWeave については、​[DataWeave バージョン 1.2 の例](#1.2@dataweave::dataweave-examples.adoc)​を参照してください。 他の DataWeave バージョンの場合は、DataWeave の目次のバージョンセレクターを使用できます。

Mule 4.2.1 リリース以降、DataWeave では、名前空間キーおよび属性を動的に生成できます。

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

- ヘッダーの名前空間定義用の ​`ns`
- XML 要素の他の属性を定義する ​`@`

DataWeave スクリプト:

```dataweave
%dw 2.0
output application/xml
ns orders http://www.acme.com/shemas/Orders
ns stores http://www.acme.com/shemas/Stores
---
root:
    orders#orders: {
        stores#shipNodeId: "SF01",
        stores#shipNodeId @(shipsVia:"LA01"): "NY03"
    }
```

出力 XML:

```xml
<?xml version='1.0' encoding='UTF-8'?>
<root>
  <orders:orders xmlns:orders="http://www.acme.com/shemas/Orders">
    <stores:shipNodeId xmlns:stores="http://www.acme.com/shemas/Stores">SF01</stores:shipNodeId>
    <stores:shipNodeId xmlns:stores="http://www.acme.com/shemas/Stores" shipsVia="LA01">NY03</stores:shipNodeId>
  </orders:orders>
</root>
```

## [](#動的に生成された名前空間の値)動的に生成された名前空間の値

_サポートの開始は Mule バージョン 4.2.1 以降_

名前空間キーおよび属性の次のサンプル出力値では、DataWeave が DataWeave 変数で定義された入力を使用してその役割を果たしています。

DataWeave スクリプト:

```dataweave
%dw 2.0
fun dynKeyNs(ns0: Namespace, value: Any) = { ns0#myDynKey: value }
fun dynAttrNs(ns0: Namespace, value: Any) = { myChildTag @(ns0#myDynAttribute: true): value }
var namespace1 = {uri: "http://acme.com", prefix: "ns0"} as Namespace
var namespace2 = {uri: "http://emca.com", prefix: "ns0"} as Namespace
output application/xml
---
root:

{
  mytagA: dynKeyNs(namespace1, "myTest1"),
  myTagB: dynKeyNs(namespace2, "myTest2"),
  myTagC: dynAttrNs(namespace1, "myTest1"),
  myTagD: dynAttrNs(namespace2, "myTest2")
}
```

出力 XML:

```xml
<?xml version='1.0' encoding='UTF-8'?>
<root>
  <mytagA>
    <ns0:myDynKey xmlns:ns0="http://acme.com">myTest1</ns0:myDynKey>
  </mytagA>
  <myTagB>
    <ns0:myDynKey xmlns:ns0="http://emca.com">myTest2</ns0:myDynKey>
  </myTagB>
  <myTagC>
    <myChildTag xmlns:ns0="http://acme.com" ns0:myDynAttribute="true">myTest1</myChildTag>
  </myTagC>
  <myTagD>
    <myChildTag xmlns:ns0="http://emca.com" ns0:myDynAttribute="true">myTest2</myChildTag>
  </myTagD>
</root>
```

## [](#ハイフン文字が含まれる-xml-名前空間の使用)ハイフン文字が含まれる XML 名前空間の使用

次の DataWeave スクリプトでは、オブジェクトとして XML 名前空間を定義する方法を示しています。これにより、ハイフン (​`-`​) などの特殊文字を名前空間の定義で使用できるようになります。

DataWeave スクリプト:

```dataweave
%dw 2.0
output application/xml
var myNS = {uri: "http://www.abc.com", prefix: "ns-0"} as Namespace
---
{myNS#bla : "dw"}
```

出力 XML:

```xml
<?xml version='1.0' encoding='UTF-8'?>
<ns-0:bla xmlns:ns-0="http://www.abc.com">dw</ns-0:bla>
```

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

- [自己終了 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)

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

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