Hear from Salesforce leaders on how to create and deploy Agentforce agents.
Contact Us 1-800-596-4880

XSLT Transformer

*Mule 3.6.0 and later*

Mule 3.6.0 and later versions support XSLT version 3.0 for the XSLT transformer. The transformer’s behavior and syntax remain unaltered from earlier versions. You can select which version of XSLT your transformer uses with the version declaration (see below). For details on XML, XSLT, Xquery and XPath 3.0 support in Mule, see XML Module Overview.

Mule relies on Saxon HE which does not support streaming in XSLT transformation. This may result in memory problems when processing large files.
As the data needed for the transformation resides in memory, starting with heap size of 5x the size of the files to processed is an option, but this depends on the transformation to be performed. The recommended option for processing large file transformations is Dataweave.

Declaring the XSLT Version

To declare the XSLT version that the transformer should use, use the version attribute in the XSL template, as shown below.

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<users>
  <xsl:copy-of select="." />
</users>
</xsl:template>
</xsl:stylesheet>
xml

Example

The following example shows how to configure an inline XSLT transformer that pulls parameters from the current message.

To use the XSLT transformer, you add it to your Mule XML configuration as follows:

<mulexml:xslt-transformer name="xslt">
  <mulexml:xslt-text>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml"/>
      <xsl:param name="title"/>
      <xsl:param name="rating"/>
      <xsl:template match="catalog">
      <xsl:element name="cd-listings">
        <xsl:attribute name="title">
          <xsl:value-of select="$title"/>
        </xsl:attribute>
        <xsl:attribute name="rating">
          <xsl:value-of select="$rating"/>
        </xsl:attribute>
        <xsl:apply-templates/>
      </xsl:element>
      </xsl:template>

      <xsl:template match="cd">
      <xsl:element name="cd-title">
        <xsl:value-of select = "title" />
      </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
  </mulexml:xslt-text>
  <mulexml:context-property key="title" value="#[header:ListTitle]"/>
  <mulexml:context-property key="rating" value="#[header:ListRating]"/>
xml

This example configures a transformer using inline XSLT expressions. It also defines two context parameters:

<mulexml:context-property key="title" value="#[header:ListTitle]"/>
<mulexml:context-property key="rating" value="#[header:ListRating]"/>
xml

These parameters are pulled from the current message and made available in the XSLT context so that they can be referenced in your XSLT statements. You can use any valid expression. In this example, the header evaluator is used to pull a header from the current message.

Your configured XSLT transformer can be referenced by an endpoint. In the following example, the result is written to System.out. The test data looks like this:

<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
xml

The result written to System.out looks like this:

<cd-listings title="MyList" rating="6">
    <cd-title>Empire Burlesque</cd-title>
    <cd-title>Hide your heart</cd-title>
    <!-- ... </cd-listings> -->
xml

The full configuration for this example is shown below.

Using Group-by Expressions on a Set of Nodes

This example leverages one of the new features introduced by XSLT 3.0: using a group-by expression to create groups to operate on.

The following XML contains a list of cities, each with its country and population:

<?xmlversion="1.0"encoding="UTF‐8"?>
<cities>
  <cityname="milan" country="italy" pop="5"/>
  <cityname="paris" country="france" pop="7"/>
  <cityname="munich"country="germany"pop="4"/>
  <cityname="lyon" country="france" pop="2"/>
  <cityname="venice"country="italy" pop="1"/>
</cities>
xml

The code sample below converts the XML to an HTML table showing each country with a comma-separated list of all its cities, followed by the sum total of their population:

<mulexml:xslt‐transformername="xslt">
    <mulexml:xslt‐text>
        <xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="2.0">
        <xsl:templatematch="/">
            <table>
            <xsl:for‐each‐groupselect="cities/city"groupby="@country">
                <tr>
                <td>
                    <xsl:value‐ofselect="@country"/>
                </td>
                <td>
                    <xsl:value‐ofselect="current‐group()/@name"separator=","/>
                </td>
                <td>
                    <xsl:value‐ofselect="sum(current‐group()/@pop)"/>
                </td>
                </tr>
            </xsl:for‐each‐group>
            </table>
        </xsl:template>
        </xsl:stylesheet>
    </mulexml:xslt‐text>
</mulexml:xslt‐transformer>
xml

The output will be similar to this:

<table>
    <tr>
        <th>Country</th>
        <th>CityList</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>
xml

Testing the Transformer

This transformer can be tested using the following functional test. Note that it uses FunctionalTestCase, which is part of Mule’s Test support.

View on GitHub