Contact Free trial Login

Include XML Namespaces

You can define different namespaces in the header and then reference them on each tag. Before you begin, note that DataWeave 2.0 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave 1.0 (%dw 1.0) examples, within the Mule 3.9 documentation set. For other Mule versions, you can use the Mule Runtime version selector in the table of contents.

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

The following example uses:

  • namespace definitions in the header.

  • @ to define other attributes in an XML element.

%dw 2.0
output application/xml
ns orders
ns stores
    orders#orders: {
        stores#shipNodeId: "SF01",
        stores#shipNodeId @(shipsVia:"LA01"): "NY03"
<?xml version='1.0' encoding='US-ASCII'?>
  <po:order xmlns:po="">
    <store:shipNodeId xmlns:store="">SF01</store:shipNodeId>
    <store:shipNodeId xmlns:store="" shipsVia="LA01">NY03</store:shipNodeId>

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.

%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: "", prefix: "ns0"} as Namespace
var namespace2 = {uri: "", prefix: "ns0"} as Namespace
output application/xml

  mytagA: dynKeyNs(namespace1, "myTest1"),
  myTagB: dynKeyNs(namespace2, "myTest2"),
  myTagC: dynAttrNs(namespace1, "myTest1"),
  myTagD: dynAttrNs(namespace2, "myTest2")
<?xml version='1.0' encoding='UTF-8'?>
    <ns0:myDynKey xmlns:ns0="">myTest1</ns0:myDynKey>
    <ns0:myDynKey xmlns:ns0="">myTest2</ns0:myDynKey>
    <myChildTag xmlns:ns0="" ns0:myDynAttribute="true">myTest1</myChildTag>
    <myChildTag xmlns:ns0="" ns0:myDynAttribute="true">myTest2</myChildTag>

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.