Nav

DataWeave XML Reference

Instead of using the Studio UI to include DataWeave language on your Mule projects, you can optionally also create and configure a 'Transform Message' component entirely via XML, either through the XML tab in Studio or via an external editor.

Much of the functionality of this component can be managed through the Transform Message Component in Anypoint Studio. However, you can also implement these settings through XML code. Certain advanced features are currently only configurable via XML.

If you use the UI to create and configure the element, the same XML configuration is created automatically in the XML editor.

DataWeave Dependencies

Namespace and Schema Location

At the very start of your Mule Project, you must include the following dependencies:


          
       
1
2
3
4
5
6
<mule

      xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
      xsi:schemaLocation="
            ...
            http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
In Studio, as occurs with other Mule components, these dependencies are automatically added when you drag and drop the corresponding element to your canvas.

Maven Dependency Snippet

If you use Maven on your project, you must also include the following dependencies on your POM maven file:


          
       
1
2
3
4
5
6
<dependency>
            <groupId>com.mulesoft.weave</groupId>
            <artifactId>mule-plugin-weave</artifactId>
            <version>${mule.version}</version>
            <scope>provided</scope>
        </dependency>
For users of Mule Runtime version 3.8.0, the aftifactID you need to use is mule-plugin-weave_2.11, rather than just mule-plugin-weave.

<dw:transform-message>

This is the parent XML tag for a Transform Message component, containing everything else.

Attributes of <dw:transform-message>

Value Description

doc:name

Defines the name that will be visible under the element in the Studio canvas.

mode

Refers to Deferred Execution. Accepted values: immedate or deferred. See DataWeave Memory Management.

Transformations

This is a child element of the parent <dw:transform-message>.

It contains the actual DataWeave transformation code. Depending on where you desire to put the output in the outgoing Mule message, you can define it via the element sw:set-payload, dw:set-variable, or dw:set-session-variable.


         
      
1
2
3
<dw:set-payload resource="classpath:path/transform.dwl"/>
<dw:set-variable variableName="myVariable" resource="classpath:path/transform.dwl"/>
<dw:set-session-variable variableName="mySessionVariable" resource="classpath:path/transform.dwl"/>

Attributes of <dw:set-payload> and other elements that describe transformations

Value Description

resource

Point to a .DWL file containing a DataWeave transform

variableName

When the desired output is a variable, this defines what to name that variable. Not applicable on dw:set-paload.

Handling Multiple Outputs

A single Transform Message element can give shape to several different components of the output Mule message. Each of these output components must be defined in a separate .dwl file. For example one may be defining the payload contents, another those of an outbound property, and these will both be parts of the same output Mule message.

You do this by adding multiple child elements inside the dw:transform-message component.


         
      
1
2
3
4
5
<dw:transform-message>
            <dw:set-payload resource="classpath:path/transform.dwl"/>
            <dw:set-variable variableName="myVariable" resource="classpath:path/transform.dwl"/>
            <dw:set-session-variable variableName="mySessionVariable" resource="classpath:path/transform.dwl"/>
</dw:transform-message>

Through the type of the child element, you specify where in the output Mule message to place the output of this new DataWeave transform. In case you’re creating a new variable or property, you must also set a name for it.

Referencing External DW Files

You can either type your DataWeave code into your XML code using C-Data within a Transformations element, or you can reference an external .dwl file.

Below is an example that spells out the DataWeave code inline:


         
      
1
2
3
4
5
6
7
8
9
10
<dw:transform-message doc:name="Transform Message" mode="immediate">
        <dw:set-payload>
                <![CDATA[
                              %dw 1.0
                        %output application/json
                        ---
                        payload
                ]]>
        </dw:set-payload >
</dw:transform-message>

Below is the same example, calling an external .dwl file:


         
      
1
2
3
<dw:transform-message doc:name="Transform Message" mode="immediate">
        <dw:set-payload resource="transform.dwl" />
</dw:transform-message>

The default location for these files is in the 'src/main/resources' folder in your project.

Input definitions

This is a child element of <dw:transform-message>.

It specifies certain optional parameters about the input.


         
      
1
<dw:input-payload mimeType="application/xml" doc:sample="sample_data/content.xml"/>

Attributes of <dw:input-payload>

Value Description

mimeType

expected mime Type of the input

doc:sample

Points to a file containing a sample input, useful at design time when constructing the transformation via the UI. See [Providing Input Sample Data].


         
      
1
2
3
4
5
6
7
8
9
10
11
12
<dw:transform-message doc:name="Transform Message">
        <dw:input-payload mimeType="application/xml" doc:sample="sample_data/content.xml"/>
        <dw:set-payload>
        <![CDATA[%dw 1.0
        %output application/java
        ---
        {
                // YOUR DW SCRIPT
        }
        ]]>
        </dw:set-payload>
</dw:transform-message>

If you do not provide this attribute, DataWeave will try to read the payload MIME type from the metadata.

Reader Configuration

This is a child element of an <dw:input-payload>.

Each of these elements defines a specific property that tells the reader how to parse the input.


         
      
1
2
<dw:reader-property name="separator" value="|"/>
<dw:reader-property name="header" value="false"/>

The reader properties that may be set vary depending on the type of the input. For a detailed list of the available properties for each type, see DataWeave formats.


         
      
1
2
3
4
<dw:input-payload doc:sample="list_csv.csv" mimeType="text/csv" >
    <dw:reader-property name="separator" value="|"/>
    <dw:reader-property name="header" value="false"/>
</dw:input-payload>

For a detailed reference of what properties can be set in the Reader Configuration of each format, see the corresponding reader properties section:

Memory Management

The dw component can be configured to handle the execution of a transformation of a large payload at a deferred time, and you can set the maximum size for which it will use memory rather than the hard disk. No configuration is necessary in the Transform Message component, but you may finetune certain parameters if you wish through the mode attribute. See DataWeave Memory Management.

Full XML Sample

Below is a full Transform Message component described via XML


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dw:transform-message doc:name="Transform Message" mode="immediate">
        <dw:input-payload mimeType="text/csv" doc:sample="sample_data/content.csv">
                <dw:reader-property name="separator" value="|"/>
                <dw:reader-property name="header" value="false"/>
        </dw:input-payload>
        <dw:set-variable variableName="myVariable">
                <![CDATA[
                        %dw 1.0
                        %output application/json
                        ---
                        payload
                ]]>
        </dw:set-variable >
</dw:transform-message>