---
schema_version: "1"
id: 3559ab57ba909469af70eb7eb729ee8586a669ddf3cc2ed10c595e9af0f1565b
title: Select XML Elements
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.11/dataweave-cookbook-select-xml-elements.md
canonical_url: https://docs.mulesoft.com/dataweave/2.11/dataweave-cookbook-select-xml-elements
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 92eaf3f3bb8fb8daf23117f48d559cbf52e37c25c9e0ba3c44a5f08a35834213
description: MuleSoft Documentation Site
release: "2.11"
latest: false
---
---
product: DataWeave
version: 2.11
keywords: studio, anypoint, transform, transformer, format, xml, json, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure
---

# Select XML Elements

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

DataWeave provides a number of [selectors](https://docs.mulesoft.com/dataweave/2.11/dataweave-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.

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:

```dataweave
%dw 2.0
output application/xml
---
payload.language
```

Input XML Payload:

```xml
<language>
    <name>DataWeave</name>
</language>
```

Output XML:

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

```dataweave
%dw 2.0
output application/xml
---
{ myroot: payload.root }
```

Input XML Payload:

```xml
<root>
    <element>
        <subelement1>SE1</subelement1>
    </element>
    <element>E2</element>
</root>
```

Output XML:

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

```dataweave
%dw 2.0
output application/xml
---
{ newname : payload.language.name }
```

Input XML Payload:

```xml
<language>
    <name>DataWeave</name>
    <version>2.0</version>
</language>
```

Output XML:

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

```dataweave
%dw 2.0
output application/xml
---
{ version : payload.language[1] }
```

Input XML Payload:

```xml
<language>
    <name>DataWeave</name>
    <version>2.0</version>
</language>
```

Output XML:

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

```dataweave
%dw 2.0
output application/xml
---
{ mysubelement : payload.root[0].subelement1 }
```

Input XML Payload:

```xml
<root>
    <element>
        <subelement1>SE1</subelement1>
    </element>
    <element>E2</element>
</root>
```

Output XML:

```xml
<?xml version='1.0' encoding='UTF-8'?>
<mysubelement>SE1</mysubelement>
```
