In our latest release we have added a new policy that allows you to run XSLT transformations on the body of a request or response. Those of you using XML or SOAP with your Azure API Management instance may find this useful to give you a great deal of control of how XML is served up to your API users. Here’s how the default policy statement looks:
<xsl-transform> <parameter name="User-Agent">@(context.Request.Headers.GetValueOrDefault("User-Agent","non-specified"))</parameter> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <xsl:param name="User-Agent" /> <xsl:template match="* | @* | node()"> <xsl:copy> <xsl:if test="self::* and not(parent::*)"> <xsl:attribute name="User-Agent"> <xsl:value-of select="$User-Agent" /> </xsl:attribute> </xsl:if> <xsl:apply-templates select="* | @* | node()" /> </xsl:copy> </xsl:template> </xsl:stylesheet> </xsl-transform>
Notice that we have included a “parameter” tag to show how you can inject context variables into your transformation.
So what can you use this for? Well one thing we can do is to clean up the XML payload that is returned from a SOAP service, creating a SOAP-to REST service. I have created some example code showing how you can stack the XSLTs – doing most of the response conversion at the API level, and then doing specific operation conversions before covering the XML to JSON. The code can be found in the policies folder of our APIM samples GitHub.
I have created an XSLT Policy video that goes into more detail showing how you can use this feature.