TransformXML
Transforms an XML document using an XSLT stylesheet. Runtime-proven on a live Marketing Cloud Engagement CloudPage — including the argument order and the way any bad input takes the whole page down.
Syntax
TransformXML(xmlDocument, xslDocument) → string
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
xmlDocument |
string | Yes | The XML content to transform |
xslDocument |
string | Yes | The XSLT stylesheet applied to the XML |
Example
%%[
VAR @xml, @xsl
SET @xml = "<greeting><who>World</who></greeting>"
SET @xsl = "<?xml version=\"1.0\"?><xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\"><xsl:output method=\"text\"/><xsl:template match=\"/greeting\">Hi [<xsl:value-of select=\"who\"/>]</xsl:template></xsl:stylesheet>"
]%%
%%=TransformXML(@xml, @xsl)=%%
Renders Hi [World].
To emit HTML that itself contains AMPscript, wrap the result in TreatAsContent so the transformed markup is evaluated rather than printed literally:
%%=TreatAsContent(TransformXML(@xml, @xsl))=%%
Return value
string — the text or markup produced by applying the stylesheet to the XML. The value is whatever the transform yields, so there is no closed set of return values to test for.
Behaviour
The XML comes first, the stylesheet second. TransformXML(@xml, @xsl) transforms and returns the result; swapping the two arguments so the stylesheet is passed first aborts the page, because a stylesheet is not valid input to the XML slot.
Any bad input takes the whole page down. AMPscript has no error handling for this function, so malformed XML, a stylesheet that is not well-formed XML, and an empty stylesheet each abort the CloudPage with HTTP 422 and discard everything rendered before the call — rather than returning an empty string or an error message. Validate both documents before calling, and keep the call late in the page so a failure loses as little rendered output as possible.
Show test script
%%[
VAR @b, @xml, @xsl, @res
SET @b = RequestParameter("b")
/* a valid XML string plus a text-output XSLT stylesheet */
SET @xml = "<greeting><who>World</who></greeting>"
SET @xsl = Concat("<?xml version=", '"', "1.0", '"', "?>")
SET @xsl = Concat(@xsl, "<xsl:stylesheet xmlns:xsl=", '"', "http://www.w3.org/1999/XSL/Transform", '"', " version=", '"', "1.0", '"', ">")
SET @xsl = Concat(@xsl, "<xsl:output method=", '"', "text", '"', "/>")
SET @xsl = Concat(@xsl, "<xsl:template match=", '"', "/greeting", '"', ">Hi [<xsl:value-of select=", '"', "who", '"', "/>]</xsl:template>")
SET @xsl = Concat(@xsl, "</xsl:stylesheet>")
/* happy path: XML first, XSL second -> renders Hi [World] */
IF @b == "ok" THEN
SET @res = TransformXML(@xml, @xsl)
OutputLine(Concat("ok=[", @res, "]"))
ENDIF
/* swapped argument order aborts: the XSL is not valid input to the XML slot */
IF @b == "order" THEN
OutputLine(Concat("order start"))
OutputLine(Concat("order=[", TransformXML(@xsl, @xml), "]"))
OutputLine(Concat("order done"))
ENDIF
/* malformed XML (unclosed element) aborts the page */
IF @b == "badxml" THEN
OutputLine(Concat("badxml start"))
OutputLine(Concat("badxml=[", TransformXML("<greeting><who>World</greeting>", @xsl), "]"))
OutputLine(Concat("badxml done"))
ENDIF
/* a non-XML stylesheet aborts the page */
IF @b == "badxsl" THEN
OutputLine(Concat("badxsl start"))
OutputLine(Concat("badxsl=[", TransformXML(@xml, "not xml at all"), "]"))
OutputLine(Concat("badxsl done"))
ENDIF
/* an empty stylesheet aborts the page */
IF @b == "emptyxsl" THEN
OutputLine(Concat("emptyxsl start"))
OutputLine(Concat("emptyxsl=[", TransformXML(@xml, ""), "]"))
OutputLine(Concat("emptyxsl done"))
ENDIF
]%%
Availability
| Platform | Available |
|---|---|
| Marketing Cloud Engagement | Yes |
| Marketing Cloud Next | No |
The official reference notes the function was designed for XML and XSL stored as Classic Content, and that supplying the stylesheet from a Content Builder block can throw. Passing literal strings — or Base64-decoded content blocks — works.
See also
BarcodeURL·BuildOptionList— other Content functions- Official reference · ampscript.guide