The <xml-module:xslt-transform> element can take an input XML document and transform it using an XSL style sheet.
Although MuleSoft supports the XSLT standard, DataWeave is the recommended tool for extracting and transforming XML documents.
This is an example XML input document:
<?xml version="1.0" encoding="UTF-8"?>
<cities>
<city name="milan" country="italy" pop="5"/>
<city name="paris" country="france" pop="7"/>
<city name="munich" country="germany" pop="4"/>
<city name="lyon" country="france" pop="2"/>
<city name="venice" country="italy" pop="1"/>
</cities>
You can transform the XML input document like this:
<xml-module:xslt-transform>
<xml-module:xslt><![CDATA[
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<table>
<tr>
<th>Country</th>
<th>City List</th>
<th>Population</th>
</tr>
<xsl:for-each-group select="cities/city" group-by="@country">
<tr>
<td>
<xsl:value-of select="@country"/>
</td>
<td>
<xsl:value-of select="current-group()/@name" separator=", "/>
</td>
<td>
<xsl:value-of select="sum(current-group()/@pop)"/>
</td>
</tr>
</xsl:for-each-group>
</table>
</xsl:template>
</xsl:stylesheet>
]]></xml-module:xslt>
</xml-module:xslt-transform>
This script outputs the following document:
<table>
<tr>
<th>Country</th>
<th>City List</th>
<th>Population</th>
</tr>
<tr>
<td>italy</td>
<td>milan, venice</td>
<td>6</td>
</tr>
<tr>
<td>france</td>
<td>paris, lyon</td>
<td>9</td>
</tr>
<tr>
<td>germany</td>
<td>munich</td>
<td>4</td>
</tr>
</table>
Although this operation looks for the input document at the message payload level by default, you can supply your own input. For example, imagine that the cities are a JSON array inside the payload. You could then first transform the JSON to XML, and then run the XSL transformation:
<xml-module:xslt-transform>
<xml-module:content><![CDATA[
%dw 2.0
input payload application/json encoding='UTF-8'
output application/xml encoding='UTF-8'
---
payload.cities
]]></xml-module:content>
<xml-module:xslt><![CDATA[
// THE XSLT
]]></xml-module:xslt>
</xml-module:xslt-transform>