Редагувати

Поділитися через


Transform XML using an XSLT

APPLIES TO: All API Management tiers

The xsl-transform policy applies an XSL transformation to XML in the request or response body.

Note

Set the policy's elements and child elements in the order provided in the policy statement. Learn more about how to set or edit API Management policies.

Policy statement

<xsl-transform>
    <parameter parameter-name="...">...</parameter>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:.../>
        <xsl:.../>
    </xsl:stylesheet>
  </xsl-transform>

Elements

Name Description Required
parameter Used to define variables used in the transform No
xsl:stylesheet Root stylesheet element. All elements and attributes defined within follow the standard XSLT specification. Yes

Usage

Usage notes

  • This policy can only be used once in a policy section.
  • Currently, this policy supports XSLT version 1.0.

Examples

Transform request body

<inbound>
    <base />
    <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>
</inbound>

Transform response body

<policies>
  <inbound>
      <base />
  </inbound>
  <outbound>
      <base />
      <xsl-transform>
          <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:output omit-xml-declaration="yes" method="xml" indent="yes" />
            <!-- Copy all nodes directly-->
            <xsl:template match="node()| @*|*">
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()|*" />
                </xsl:copy>
            </xsl:template>
          </xsl:stylesheet>
    </xsl-transform>
  </outbound>
</policies>

For more information about working with policies, see: