TransformXML

This function applies an XSL transformation to the specified XML data.

Arguments

TransformXML(1,2)

Ordinal Type Required Description
1 String True XML content to transform
2 String True XSL document used to transform the XML content

NOTE: This function supports the XPATH 1.0 specification.

TIP: Generally speaking, XSL transformations are powerful and flexible when it comes to transforming XML, especially if you leverage AMPscript within them. However, it’s advisable to limit the use of these in Marketing Cloud. The emails that use them can be particularly hard to maintain as the HTML code is not readily changeable in the Classic Content and Content Builder editors. It is recommended that the development of XSL emails should be done outside of Marketing Cloud using an appropriate IDE.

NOTE: The TransformXML function as illustrated in Example 1 below was designed to work with XSL templates that are uploaded as Portfolio items in Classic Content. Referencing XSL templates as Content Blocks will result in an error in the current release of Marketing Cloud. See Example 2 for a work-around for using this function with Content Builder assets.

Example 1

Below is a sample XSL file named LoyaltyRewards.xsl that has been uploaded to the Portfolio. It has a Customer/External Key of LoyaltyRewards. This XSL file retrieves the elements from the XML, and outputs the repeating reward line items in a different order. It also totals all of the reward amounts and displays them in the last row of the table.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/loyaltyRewards">
    <p>Hi, %%=properCase('<xsl:value-of select="firstName"/>')=%%!</p>
    <p>Here's your reward statement:</p>
    <table cellspacing="0" cellpadding="5" border="1">
        <tr>
            <th>Reward Date</th>
            <th>Name</th>
            <th>Description</th>
            <th>Amount</th>
        </tr>
        <xsl:for-each select="rewards/reward">
            <xsl:sort select="./rewardDate" order="descending" data-type="text"/>
            <tr>
                <td><xsl:value-of select="rewardDate"/></td>
                <td><xsl:value-of select="name"/></td>
                <td><xsl:value-of select="description"/></td>
                <td align="right"><xsl:value-of select="amount"/></td>
            </tr>
        </xsl:for-each>
        <tr>
            <td colspan="3" align="right"><b>Total</b></td>
            <td align="right"><b><xsl:value-of select="format-number(sum(rewards/reward/amount), '0.00')"/></b></td>
        </tr>
    </table>
  </xsl:template>
</xsl:stylesheet>

The TransformXML function will apply the logic in the XSL to the XML payload specified. The TreatAsContent function causes the AMPscript to be evaluated in the result.

%%[

var @xml, @xsl
set @xsl = GetPortfolioItem("LoyaltyRewards")

set @xml = AttributeValue("XML") /* value from attribute or DE column in send context */
set @xml = "" /* or a literal value */
set @xml = concat(@xml,'<?xml version="1.0"?>')
set @xml = concat(@xml,'<loyaltyRewards>')
set @xml = concat(@xml,'    <emailAddress><![CDATA[suzy@limedash.com]]></emailAddress>')
set @xml = concat(@xml,'    <firstName><![CDATA[Suzy]]></firstName>')
set @xml = concat(@xml,'    <rewards>')
set @xml = concat(@xml,'        <reward>')
set @xml = concat(@xml,'            <rewardDate><![CDATA[2017-10-15]]></rewardDate>')
set @xml = concat(@xml,'            <name><![CDATA[Purchase]]></name>')
set @xml = concat(@xml,'            <description><![CDATA[Purchase cash back]]></description>')
set @xml = concat(@xml,'            <amount><![CDATA[3.21]]></amount>')
set @xml = concat(@xml,'        </reward>')
set @xml = concat(@xml,'        <reward>')
set @xml = concat(@xml,'            <rewardDate><![CDATA[2017-10-01]]></rewardDate>')
set @xml = concat(@xml,'            <name><![CDATA[Visit]]></name>')
set @xml = concat(@xml,'            <description><![CDATA[October visit reward]]></description>')
set @xml = concat(@xml,'            <amount><![CDATA[12.34]]></amount>')
set @xml = concat(@xml,'        </reward>')
set @xml = concat(@xml,'        <reward>')
set @xml = concat(@xml,'            <rewardDate><![CDATA[2017-12-01]]></rewardDate>')
set @xml = concat(@xml,'            <name><![CDATA[Referral]]></name>')
set @xml = concat(@xml,'            <description><![CDATA[Referral bonus]]></description>')
set @xml = concat(@xml,'            <amount><![CDATA[43.21]]></amount>')
set @xml = concat(@xml,'        </reward>')
set @xml = concat(@xml,'    </rewards>')
set @xml = concat(@xml,'</loyaltyRewards>')

]%%
%%=TreatAsContent(TransformXML(@xml,@xsl))=%%

Output

<p>Hi, Suzy!</p>
<p>Here's your reward statement:</p>
<table cellspacing="0" cellpadding="5" border="1">
  <tr>
    <th>Reward Date</th>
    <th>Name</th>
    <th>Description</th>
    <th>Amount</th>
  </tr>
  <tr>
    <td>2017-12-01</td>
    <td>Referral</td>
    <td>Referral bonus</td>
    <td align="right">43.21</td>
  </tr>
  <tr>
    <td>2017-10-15</td>
    <td>Purchase</td>
    <td>Purchase cash back</td>
    <td align="right">3.21</td>
  </tr>
  <tr>
    <td>2017-10-01</td>
    <td>Visit</td>
    <td>October visit reward</td>
    <td align="right">12.34</td>
  </tr>
  <tr>
    <td colspan="3" align="right"><b>Total</b></td>
    <td align="right"><b>58.76</b></td>
  </tr>
</table>

Example 2

Here is a method for leveraging XSL and XML content using Content Builder. While it may not be reasonable in a workflow to Base64 encode XSL and XML assets, it does enable the use Content Builder as a source for this function.


Not a subscriber? Subscribe now.