---
schema_version: "1"
id: 91cd67fc12496a7223b56cbb3fd17fc651d5cf4228d17a3ccab1c0659c974afb
title: Include XML Namespaces
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.12/dataweave-cookbook-include-xml-namespaces.md
canonical_url: https://docs.mulesoft.com/dataweave/2.12/dataweave-cookbook-include-xml-namespaces
source_site: docs.mulesoft.com
content_type: documentation
content_hash: c305b678cbc879b9267843734c35d2ba3b3f71ac0868cd8861e5eb6e835afe73
description: MuleSoft Documentation Site
release: "2.12"
latest: false
---
---
product: DataWeave
version: 2.12
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
---

# Include XML Namespaces

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

You can define different namespaces in the header and then reference them on each tag.

Starting in the Mule 4.2.1 release, DataWeave also supports dynamically generated namespace keys and
attributes.

The following example uses:

- `ns` for namespace definitions in the header

- `@` to define other attributes in an XML element

DataWeave Script:

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

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

## Dynamically Generated Namespace Values

_Support starting in Mule version 4.2.1_

Using input defined in the DataWeave variables, the DataWeave functions in the following example
output values for namespace keys and attributes.

DataWeave Script:

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

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

## Use XML Namespace with Hyphen Character

The following DataWeave script shows you how to define an XML namespace as an object, which enables
you to use special characters such as hyphen (`-`) in the namespace definition:

DataWeave Script:

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

Output XML:

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

## Related Examples

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

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

## See Also

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

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