%dw 2.0
output application/xml
---
payload update {
case user at .users.*user -> user update {
case .personal_information.first_name -> upper(user.personal_information.first_name)
case .personal_information.middle_name -> upper(user.personal_information.middle_name)
case .personal_information.last_name -> upper(user.personal_information.last_name)
}
}
Change the Value of a Field
The following DataWeave examples show how to use update
and mask
to change the values of some 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.
Example: Using Update to Change Values
This example uses:
-
update
to update specified fields of the payload with new values -
single-value (
.
) and multi-value (.*
) selectors to navigate the payload and select the fields to update
The following script shows how the update
operator works. The example creates a new users
list by updating each user
in the payload with a new user
. The transformation converts first_name
, middle_name
and last_name
to uppercase values:
<users>
<user>
<personal_information>
<first_name>Emiliano</first_name>
<middle_name>Romoaldo</middle_name>
<last_name>Lesende</last_name>
<ssn>001-08-84382</ssn>
</personal_information>
<login_information>
<username>3miliano</username>
<password>mypassword1234</password>
</login_information>
</user>
<user>
<personal_information>
<first_name>Mariano</first_name>
<middle_name>Toribio</middle_name>
<last_name>de Achaval</last_name>
<ssn>002-05-34738</ssn>
</personal_information>
<login_information>
<username>machaval</username>
<password>mypassword4321</password>
</login_information>
</user>
</users>
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<personal_information>
<first_name>EMILIANO</first_name>
<middle_name>ROMOALDO</middle_name>
<last_name>LESENDE</last_name>
<ssn>001-08-84382</ssn>
</personal_information>
<login_information>
<username>3miliano</username>
<password>mypassword1234</password>
</login_information>
</user>
<user>
<personal_information>
<first_name>MARIANO</first_name>
<middle_name>TORIBIO</middle_name>
<last_name>DE ACHAVAL</last_name>
<ssn>002-05-34738</ssn>
</personal_information>
<login_information>
<username>machaval</username>
<password>mypassword4321</password>
</login_information>
</user>
</users>
Example: Using Mask to Change Values
DataWeave provides a simple way to mask values, without specifying the path to each field:
-
mask
updates all simple elements that match the selected name throughout the input with the specified mask.
The following example masks the ssn
and password
values with a set of asterisks (****
):
%dw 2.0
import * from dw::util::Values
output application/xml
---
(payload mask "ssn" with "****") mask "password" with "****"
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<personal_information>
<first_name>EMILIANO</first_name>
<middle_name>ROMOALDO</middle_name>
<last_name>LESENDE</last_name>
<ssn>001-08-84382</ssn>
</personal_information>
<login_information>
<username>3miliano</username>
<password>mypassword1234</password>
</login_information>
</user>
<user>
<personal_information>
<first_name>MARIANO</first_name>
<middle_name>TORIBIO</middle_name>
<last_name>DE ACHAVAL</last_name>
<ssn>002-05-34738</ssn>
</personal_information>
<login_information>
<username>machaval</username>
<password>mypassword4321</password>
</login_information>
</user>
</users>
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<personal_information>
<first_name>EMILIANO</first_name>
<middle_name>ROMOALDO</middle_name>
<last_name>LESENDE</last_name>
<ssn>****</ssn>
</personal_information>
<login_information>
<username>3miliano</username>
<password>****</password>
</login_information>
</user>
<user>
<personal_information>
<first_name>MARIANO</first_name>
<middle_name>TORIBIO</middle_name>
<last_name>DE ACHAVAL</last_name>
<ssn>****</ssn>
</personal_information>
<login_information>
<username>machaval</username>
<password>****</password>
</login_information>
</user>
</users>