{"id":31428,"date":"2018-04-21T15:47:46","date_gmt":"2018-04-21T15:47:46","guid":{"rendered":"https:\/\/ampscript.guide\/transformxml\/"},"modified":"2023-06-22T13:09:04","modified_gmt":"2023-06-22T13:09:04","slug":"transformxml","status":"publish","type":"post","link":"https:\/\/ampscript.guide\/transformxml\/","title":{"rendered":"TransformXML"},"content":{"rendered":"<h2>TransformXML<\/h2>\n<p>This function applies an XSL transformation to the specified XML data.<\/p>\n<h3>Arguments<\/h3>\n<p><code>TransformXML(1,2)<\/code><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">Ordinal<\/th>\n<th style=\"text-align: left;\">Type<\/th>\n<th style=\"text-align: left;\">Required<\/th>\n<th style=\"text-align: left;\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: left;\">String<\/td>\n<td style=\"text-align: left;\">True<\/td>\n<td style=\"text-align: left;\">XML content to transform<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">2<\/td>\n<td style=\"text-align: left;\">String<\/td>\n<td style=\"text-align: left;\">True<\/td>\n<td style=\"text-align: left;\">XSL document used to transform the XML content<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p>NOTE: This function supports the <a href=\"https:\/\/www.w3.org\/TR\/1999\/REC-xpath-19991116\/\">XPATH 1.0 specification<\/a>.<\/p>\n<p>TIP: Generally speaking, XSL transformations are powerful and flexible when it comes to transforming XML, especially if you leverage AMPscript within them.  However, it&#8217;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.<\/p>\n<p>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 <a href=\"https:\/\/issues.salesforce.com\/issue\/a028c00000gAzCWAA0\/it-is-not-possible-to-use-the-ampscript-function-transformxml-with-contentbuilder-assets\">result in an error<\/a> in the current release of Marketing Cloud. See Example 2 for a work-around for using this function with Content Builder assets.<\/p>\n<\/blockquote>\n<h3>Example 1<\/h3>\n<p>Below is a sample XSL file named <code>LoyaltyRewards.xsl<\/code> that has been uploaded to the Portfolio.  It has a Customer\/External Key of <code>LoyaltyRewards<\/code>.  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.<\/p>\n<pre><code>&lt;?xml version=\"1.0\"?&gt;\n&lt;xsl:stylesheet xmlns:xsl=\"http:\/\/www.w3.org\/1999\/XSL\/Transform\" version=\"1.0\"&gt;\n    &lt;xsl:output method=\"html\" indent=\"yes\"\/&gt;\n    &lt;xsl:template match=\"\/loyaltyRewards\"&gt;\n    &lt;p&gt;Hi, %%=properCase('&lt;xsl:value-of select=\"firstName\"\/&gt;')=%%!&lt;\/p&gt;\n    &lt;p&gt;Here's your reward statement:&lt;\/p&gt;\n    &lt;table cellspacing=\"0\" cellpadding=\"5\" border=\"1\"&gt;\n        &lt;tr&gt;\n            &lt;th&gt;Reward Date&lt;\/th&gt;\n            &lt;th&gt;Name&lt;\/th&gt;\n            &lt;th&gt;Description&lt;\/th&gt;\n            &lt;th&gt;Amount&lt;\/th&gt;\n        &lt;\/tr&gt;\n        &lt;xsl:for-each select=\"rewards\/reward\"&gt;\n            &lt;xsl:sort select=\".\/rewardDate\" order=\"descending\" data-type=\"text\"\/&gt;\n            &lt;tr&gt;\n                &lt;td&gt;&lt;xsl:value-of select=\"rewardDate\"\/&gt;&lt;\/td&gt;\n                &lt;td&gt;&lt;xsl:value-of select=\"name\"\/&gt;&lt;\/td&gt;\n                &lt;td&gt;&lt;xsl:value-of select=\"description\"\/&gt;&lt;\/td&gt;\n                &lt;td align=\"right\"&gt;&lt;xsl:value-of select=\"amount\"\/&gt;&lt;\/td&gt;\n            &lt;\/tr&gt;\n        &lt;\/xsl:for-each&gt;\n        &lt;tr&gt;\n            &lt;td colspan=\"3\" align=\"right\"&gt;&lt;b&gt;Total&lt;\/b&gt;&lt;\/td&gt;\n            &lt;td align=\"right\"&gt;&lt;b&gt;&lt;xsl:value-of select=\"format-number(sum(rewards\/reward\/amount), '0.00')\"\/&gt;&lt;\/b&gt;&lt;\/td&gt;\n        &lt;\/tr&gt;\n    &lt;\/table&gt;\n  &lt;\/xsl:template&gt;\n&lt;\/xsl:stylesheet&gt;<\/code><\/pre>\n<p>The TransformXML function will apply the logic in the XSL to the XML payload specified.  The <a href=\"\/treatascontent\">TreatAsContent<\/a> function causes the AMPscript to be evaluated in the result.<\/p>\n<pre><code>%%[\n\nvar @xml, @xsl\nset @xsl = GetPortfolioItem(\"LoyaltyRewards\")\n\nset @xml = AttributeValue(\"XML\") \/* value from attribute or DE column in send context *\/\nset @xml = \"\" \/* or a literal value *\/\nset @xml = concat(@xml,'&lt;?xml version=\"1.0\"?&gt;')\nset @xml = concat(@xml,'&lt;loyaltyRewards&gt;')\nset @xml = concat(@xml,'    &lt;emailAddress&gt;&lt;![CDATA[suzy@limedash.com]]&gt;&lt;\/emailAddress&gt;')\nset @xml = concat(@xml,'    &lt;firstName&gt;&lt;![CDATA[Suzy]]&gt;&lt;\/firstName&gt;')\nset @xml = concat(@xml,'    &lt;rewards&gt;')\nset @xml = concat(@xml,'        &lt;reward&gt;')\nset @xml = concat(@xml,'            &lt;rewardDate&gt;&lt;![CDATA[2017-10-15]]&gt;&lt;\/rewardDate&gt;')\nset @xml = concat(@xml,'            &lt;name&gt;&lt;![CDATA[Purchase]]&gt;&lt;\/name&gt;')\nset @xml = concat(@xml,'            &lt;description&gt;&lt;![CDATA[Purchase cash back]]&gt;&lt;\/description&gt;')\nset @xml = concat(@xml,'            &lt;amount&gt;&lt;![CDATA[3.21]]&gt;&lt;\/amount&gt;')\nset @xml = concat(@xml,'        &lt;\/reward&gt;')\nset @xml = concat(@xml,'        &lt;reward&gt;')\nset @xml = concat(@xml,'            &lt;rewardDate&gt;&lt;![CDATA[2017-10-01]]&gt;&lt;\/rewardDate&gt;')\nset @xml = concat(@xml,'            &lt;name&gt;&lt;![CDATA[Visit]]&gt;&lt;\/name&gt;')\nset @xml = concat(@xml,'            &lt;description&gt;&lt;![CDATA[October visit reward]]&gt;&lt;\/description&gt;')\nset @xml = concat(@xml,'            &lt;amount&gt;&lt;![CDATA[12.34]]&gt;&lt;\/amount&gt;')\nset @xml = concat(@xml,'        &lt;\/reward&gt;')\nset @xml = concat(@xml,'        &lt;reward&gt;')\nset @xml = concat(@xml,'            &lt;rewardDate&gt;&lt;![CDATA[2017-12-01]]&gt;&lt;\/rewardDate&gt;')\nset @xml = concat(@xml,'            &lt;name&gt;&lt;![CDATA[Referral]]&gt;&lt;\/name&gt;')\nset @xml = concat(@xml,'            &lt;description&gt;&lt;![CDATA[Referral bonus]]&gt;&lt;\/description&gt;')\nset @xml = concat(@xml,'            &lt;amount&gt;&lt;![CDATA[43.21]]&gt;&lt;\/amount&gt;')\nset @xml = concat(@xml,'        &lt;\/reward&gt;')\nset @xml = concat(@xml,'    &lt;\/rewards&gt;')\nset @xml = concat(@xml,'&lt;\/loyaltyRewards&gt;')\n\n]%%\n%%=TreatAsContent(TransformXML(@xml,@xsl))=%%\n<\/code><\/pre>\n<h4>Output<\/h4>\n<pre><code>&lt;p&gt;Hi, Suzy!&lt;\/p&gt;\n&lt;p&gt;Here's your reward statement:&lt;\/p&gt;\n&lt;table cellspacing=\"0\" cellpadding=\"5\" border=\"1\"&gt;\n  &lt;tr&gt;\n    &lt;th&gt;Reward Date&lt;\/th&gt;\n    &lt;th&gt;Name&lt;\/th&gt;\n    &lt;th&gt;Description&lt;\/th&gt;\n    &lt;th&gt;Amount&lt;\/th&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;2017-12-01&lt;\/td&gt;\n    &lt;td&gt;Referral&lt;\/td&gt;\n    &lt;td&gt;Referral bonus&lt;\/td&gt;\n    &lt;td align=\"right\"&gt;43.21&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;2017-10-15&lt;\/td&gt;\n    &lt;td&gt;Purchase&lt;\/td&gt;\n    &lt;td&gt;Purchase cash back&lt;\/td&gt;\n    &lt;td align=\"right\"&gt;3.21&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;2017-10-01&lt;\/td&gt;\n    &lt;td&gt;Visit&lt;\/td&gt;\n    &lt;td&gt;October visit reward&lt;\/td&gt;\n    &lt;td align=\"right\"&gt;12.34&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td colspan=\"3\" align=\"right\"&gt;&lt;b&gt;Total&lt;\/b&gt;&lt;\/td&gt;\n    &lt;td align=\"right\"&gt;&lt;b&gt;58.76&lt;\/b&gt;&lt;\/td&gt;\n  &lt;\/tr&gt;\n&lt;\/table&gt;<\/code><\/pre>\n<h3>Example 2<\/h3>\n<p>Here is a method for leveraging XSL and XML content using Content Builder.  While it may not be reasonable in a workflow to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Base64\">Base64 encode<\/a> XSL and XML assets, it does enable the use Content Builder as a source for this function.<\/p>\n<div class=\"rcp_restricted rcp_paid_only\"><div>\n\n\n\t\n\t<form id=\"rcp_login_form\"  class=\"rcp_form\" method=\"POST\" action=\"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31428\/\">\n\n\t\t\n\t\t<fieldset class=\"rcp_login_data\">\n\t\t\t<p>\n\t\t\t\t<label for=\"rcp_user_login\">Username or Email<\/label>\n\t\t\t\t<input name=\"rcp_user_login\" id=\"rcp_user_login\" class=\"required\" type=\"text\"\/>\n\t\t\t<\/p>\n\t\t\t<p>\n\t\t\t\t<label for=\"rcp_user_pass\">Password<\/label>\n\t\t\t\t<input name=\"rcp_user_pass\" id=\"rcp_user_pass\" class=\"required\" type=\"password\"\/>\n\t\t\t<\/p>\n\t\t\t\t\t\t<p>\n\t\t\t\t<input type=\"checkbox\" name=\"rcp_user_remember\" id=\"rcp_user_remember\" value=\"1\"\/>\n\t\t\t\t<label for=\"rcp_user_remember\">Remember me<\/label>\n\t\t\t<\/p>\n\t\t\t<p class=\"rcp_lost_password\"><a href=\"\/wp-json\/wp\/v2\/posts\/31428?rcp_action=lostpassword\">Lost your password?<\/a><\/p>\n\t\t\t<p>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_action\" value=\"login\"\/>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_redirect\" value=\"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31428\/\"\/>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_login_nonce\" value=\"213020500f\"\/>\n\t\t\t\t<input id=\"rcp_login_submit\" class=\"rcp-button\" type=\"submit\" value=\"Login\"\/>\n\t\t\t<\/p>\n\t\t\t\t\t<\/fieldset>\n\n\t\t\n\t<\/form>\n<br \/>\nNot a subscriber? <a href=\"\/subscriptions\/subscribe\/\">Subscribe now<\/a>.\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v14.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The AMPscript Guide - TransformXML<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ampscript.guide\/transformxml\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The AMPscript Guide - TransformXML\" \/>\n<meta property=\"og:description\" content=\"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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ampscript.guide\/transformxml\/\" \/>\n<meta property=\"og:site_name\" content=\"The AMPscript Guide\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-21T15:47:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-22T13:09:04+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ampscript.guide\/#website\",\"url\":\"https:\/\/ampscript.guide\/\",\"name\":\"The AMPscript Guide\",\"description\":\"The Definitive Scripting Manual for Salesforce Marketing Cloud\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/ampscript.guide\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/transformxml\/#webpage\",\"url\":\"https:\/\/ampscript.guide\/transformxml\/\",\"name\":\"The AMPscript Guide - TransformXML\",\"isPartOf\":{\"@id\":\"https:\/\/ampscript.guide\/#website\"},\"datePublished\":\"2018-04-21T15:47:46+00:00\",\"dateModified\":\"2023-06-22T13:09:04+00:00\",\"author\":{\"@id\":\"https:\/\/ampscript.guide\/#\/schema\/person\/5335042f77731e84f9808aecef25daec\"},\"breadcrumb\":{\"@id\":\"https:\/\/ampscript.guide\/transformxml\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ampscript.guide\/transformxml\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ampscript.guide\/transformxml\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/\",\"url\":\"https:\/\/ampscript.guide\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/transformxml\/\",\"url\":\"https:\/\/ampscript.guide\/transformxml\/\",\"name\":\"TransformXML\"}}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/ampscript.guide\/#\/schema\/person\/5335042f77731e84f9808aecef25daec\",\"name\":\"dev\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","_links":{"self":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31428"}],"collection":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/comments?post=31428"}],"version-history":[{"count":0,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31428\/revisions"}],"wp:attachment":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/media?parent=31428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/categories?post=31428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/tags?post=31428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}