TransformXML

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

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: Marketing Cloud supports the XPATH 1.0 specification.

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

Example

Here is a sample XSL file named LoyaltyRewards.xsl which is 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>

If an XML payload is specified, the TransformXML function will apply the logic in the XSL to the XML payload. The TreatAsContent function forces 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[[email protected]]]></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>