---
schema_version: "1"
id: 64483dd24332144df4e44117c446bfb0adf1d6e8c31ea325a902b5f36d2ba9b8
title: XSLT Transformations with the XML Module
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/xml-module/1.3/xml-xslt.md
canonical_url: https://docs.mulesoft.com/xml-module/1.3/xml-xslt
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 3b8d7f6bad158a8cf815b5fa30cee39f91b828027f938b3ebcde70d0f5cb057d
description: MuleSoft Documentation Site
release: "1.3"
latest: false
---
---
product: XML Module
version: 1.3
---

# XSLT Transformations with the XML Module

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

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

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

## Externalizing the XSL Style Sheet to a Separate File

To avoid embedding the XSLT script in your Mule app, you can pass it to the module through a file,
for example:

```xml
<xml-module:xslt-transform>
    <xml-module:xslt>${file::xslt/cities.xslt}</xml-module:xslt>
</xml-module:xslt-transform>
```

In a more complex use case, the script you use might depend on some external condition. For example,
imagine a multi-tenant integration in which the transformation to use depends on a `userId`:

```xml
<flow name="multitenantExample">
    <http:listener path="transform" allowedMethods="POST" config-ref="httpListener" /> // 1
    <file:read path="#['xslt/$(attributes.queryParam.userId).xslt']" target="xslt" />
    <xml-module:xslt-transform>
        <xml-module:xslt>#[vars.xslt]</xml-module:xslt>
    </xml-module:xslt-transform>
</flow>
```

1.  The example flow is triggered through an HTTP request.

2.  The example is based on the `userId` value being provided as a query parameter and the File
    Connector being used to read the file that contains the correct XSL and store it in a variable.

3.  The example then executes a transformation using an expression to point to the XSL sheet
    obtained through the `Read` operation.

The HTTP and File Connectors are used as examples and are not required to perform similar use cases.

## Using Context Properties

The XML module, like the XSL language, provides context properties that can pass parameters to the
transformation. In this example, the `<xml-module:context-properties/>` element contains a DataWeave
expression that constructs an arguments map.

```xml
<xml-module:xslt-transform>
    <xml-module:xslt><![CDATA[
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
        <xsl:output method="xml"/>

        <xsl:param name="vtn" /> (1)
        <xsl:template match="/parameter">
            <param><xsl:value-of select="$vtn"/></param> (2)
        </xsl:template>
    </xsl:stylesheet>
]]></xml-module:xslt>
    <xml-module:context-properties>#[{'vtn' : 'some value'}] (3)
    </xml-module:context-properties>
</xml-module:xslt-transform>
```

1.  The XSL style sheet declares a `param` called `vtn`.

2.  The style sheet references the assigned value of the `param` using the `$` prefix.

3.  The example uses the `context-properties` parameter to pass in the values.

## Output MIME Type

The most common use case for XSLT is to output a new XML document. However, XSLT can produce
documents in other formats, such as JSON, CSV, or a text or number value, which means that the
produced value does not have any specific MIME type assigned. You should specify a MIME type using
the `outputMimeType` parameter.

## See Also

[XML Module Overview](https://docs.mulesoft.com/xml-module/1.3/)
