Contact Us 1-800-596-4880

DataWeave XML Reference

Instead of using the Studio UI to add a DataWeave script to your Mule projects, you can write a Transform Message component in XML, either through the XML tab in Studio or with an external editor. In fact, certain advanced features (such as the mode attribute used for memory management) are only configurable through the XML.

Note that when you use the Studio UI to create and configure the element, the same XML configuration is created automatically in the XML editor.

DataWeave Dependencies

Namespace and Schema Location

Your Mule project must begin with the following dependencies:

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

Note that these dependencies are automatically added when you drag and drop the corresponding element to your Studio canvas.

Maven Dependency Snippet

If you use Maven on your project, you must also include the following dependencies on your pom.xml Maven file:

<dependency>
  <groupId>com.mulesoft.weave</groupId>
  <artifactId>mule-plugin-weave</artifactId>
  <version>${mule.version}</version>
  <scope>provided</scope>
</dependency>

Note that for Mule Runtime version 3.8.0, the artifactID is mule-plugin-weave_2.11, not mule-plugin-weave.

<dw:transform-message/>

The <dw:transform-message> element is the top-level XML tag for the Transform Message component.

<dw:transform-message> Attributes Description

doc:name

Defines the name of the element as it appears in the Studio canvas.

mode

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

Adding DataWeave Scripts to the Transform Component

You can either type your DataWeave code into your XML using CDATA within a Transformation element, or you can reference an external .dwl file.

This example that writes a DataWeave script inline within a <dw:set-payload> transformation element:

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

Here is same example, calling an external .dwl file:

<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.

Transformations

You can pass DataWeave scripts through these child elements to <dw:transform-message>: <dw:set-payload/>, <dw:set-variable/>, or <dw:set-session-variable/>.

Example
<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>

The child elements contain many of the same attributes.

Attributes Description

resource

Point to a .dwl file containing a DataWeave transformation script. These values are solved statically. For example, using <dw:set-payload resource="myscript-${env}.dwl"/> generates this error in the Transform Message UI: Unexpected end of input, expected header or content.

variableName

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

Handling Multiple Outputs

A single Transform Message element contain several different components that provide content for the output Mule message. Each of the outputs must be defined in a separate .dwl file, as shown in Transformations. For example, one file might define the payload contents, another an outbound property, another a session variable. Each will be part of the same output Mule message.

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

Input definitions

<dw:input-payload/> can specify optional parameters about the input, for example:

Attributes of <dw:input-payload> Description

mimeType

Expected mime type of the input. If you do not provide this attribute, DataWeave will try to read the payload mime type from the metadata.

doc:sample

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

Example
<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>

Reader Configuration

<dw:reader-property/> settings can tell the reader how to parse the input.

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

Reader properties depend on the type of the input. For a detailed list of the available properties for each type, see DataWeave Formats.

Full XML Sample

Below is a full Transform Message component described via XML

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

Memory Management

The mode attribute of the Transform component (dw:transform-message) can be configured to wait to execute the transformation of a large payload until a stream is available for it. You can also set the maximum size of system memory usage for the transformation. For details, see DataWeave Memory Management.