Contact Us 1-800-596-4880

Select XML Elements

DataWeave provides a number of selectors for traversing the structure of input data and returning matching values. The following examples use a single-value DataWeave selector (.) to extract data from XML elements.

Before you begin, note that DataWeave version 2 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave version 1 (%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.

The XML markup language requires a single root element. In the following example, the selector on the root (language) is able to extract the child element because the child has no siblings.

DataWeave Script:
%dw 2.0
output application/xml
---
payload.language
Input XML Payload:
<language>
    <name>DataWeave</name>
</language>
Output XML:
<?xml version='1.0' encoding='UTF-8'?>
<name>DataWeave</name>

To return all subelements of the root element in a valid XML structure with a single root, the following script uses a DataWeave key to specify a new element name (myroot) and selects the root (root) to return its value. A script that fails to return a single XML root produces the following error: Trying to output non-whitespace characters outside main element tree (in prolog or epilog)…​.

DataWeave Script:
%dw 2.0
output application/xml
---
{ myroot: payload.root }
Input XML Payload:
<root>
    <element>
        <subelement1>SE1</subelement1>
    </element>
    <element>E2</element>
</root>
Output XML:
<?xml version='1.0' encoding='UTF-8'?>
<myroot>
  <element>
    <subelement1>SE1</subelement1>
  </element>
  <element>E2</element>
</myroot>

The following example uses the name of the child element (name) with the single-value selector to select its value. Notice how payload.language.name navigates to the child element. The script also provides a root element for the value so that DataWeave can construct valid XML.

DataWeave Script:
%dw 2.0
output application/xml
---
{ newname : payload.language.name }
Input XML Payload:
<language>
    <name>DataWeave</name>
    <version>2.0</version>
</language>
Output XML:
<?xml version='1.0' encoding='UTF-8'?>
<newname>DataWeave</newname>

The following example uses an index to return the value of the second child element from the input XML. Notice that DataWeave treats child elements as indices of an array, so it can select the second child by using the index language[1]. The result is a single XML element.

DataWeave Script:
%dw 2.0
output application/xml
---
{ version : payload.language[1] }
Input XML Payload:
<language>
    <name>DataWeave</name>
    <version>2.0</version>
</language>
Output XML:
<?xml version='1.0' encoding='UTF-8'?>
<version>2.0</version>

If multiple child elements of the input have the same name, use the index to select the value of the child. The following example selects a subelement of a child element.

DataWeave Script:
%dw 2.0
output application/xml
---
{ mysubelement : payload.root[0].subelement1 }
Input XML Payload:
<root>
    <element>
        <subelement1>SE1</subelement1>
    </element>
    <element>E2</element>
</root>
Output XML:
<?xml version='1.0' encoding='UTF-8'?>
<mysubelement>SE1</mysubelement>