使用 XSLT 转换 XML

xsl-transform 策略在请求或响应正文中将 XSL 转换应用到 XML。

注意

按照策略声明中提供的顺序设置策略的元素和子元素。 详细了解如何设置或编辑 API 管理策略

策略语句

<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>

元素

名称 说明 必需
参数 用于定义在转换中使用的变量
xsl:stylesheet 根样式表元素。 在其中定义的所有元素和属性都遵循标准的 XSLT 规范

使用情况

使用注意事项

  • 此策略只能在策略部分中使用一次。
  • 目前,此策略支持 XSLT 1.0 版本。

示例

转换请求正文

<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>

转换响应正文

<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>

后续步骤

有关使用策略的详细信息,请参阅: