Skip to content

Latest commit

 

History

History
124 lines (90 loc) · 5.99 KB

File metadata and controls

124 lines (90 loc) · 5.99 KB
description
This page provides the technical details of the XSLT policy

XSLT

{% hint style="warning" %} This feature requires Gravitee's Enterprise Edition. {% endhint %}

Overview

You can use the xslt policy to apply an XSL transformation to an incoming XML request body or to the response body if your backend is exposing XML content.

This policy is based on the Saxon library.

By default, a DOCTYPE declaration will cause an error. This is for security. If you want to allow it, you can set policy.xslt.secure-processing to false in the Gateway configuration file (gravitee.yml).

Functional and implementation information for the xslt policy is organized into the following sections:

Examples

{% hint style="warning" %} This policy can be applied to v2 APIs and v4 proxy APIs. It cannot be applied to v4 message APIs. {% endhint %}

{% tabs %} {% tab title="Proxy API example" %} Remove SOAP elements when calling a WS:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    exclude-result-prefixes="fn xsl">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

    <!-- template to copy elements -->
    <xsl:template match="*">
        <xsl:if test="normalize-space(string(.)) != ''">
            <xsl:element name="{local-name()}">
                <xsl:apply-templates select="@* | node()"/>
            </xsl:element>
        </xsl:if>
    </xsl:template>

    <!-- template to copy attributes -->
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

    <!-- template to copy the rest of the nodes -->
    <xsl:template match="comment() | text() | processing-instruction()">
        <xsl:copy/>
    </xsl:template>

    <xsl:template match="soapenv:*">
        <xsl:apply-templates select="@* | node()" />
    </xsl:template>

    <xsl:template match="@xsi:nil[.='true']"/>
</xsl:stylesheet>

{% endtab %} {% endtabs %}

Configuration

Sample policy configuration:

{% code title="Sample Configuration" %}

"xslt": {
    "scope": "RESPONSE",
    "stylesheet": "<xsl:stylesheet \n  version=\"2.0\"\n  xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"\n  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"   xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:fn=\"http://www.w3.org/2005/xpath-functions\" exclude-result-prefixes=\"fn xsl\">\n  <xsl:output method=\"xml\" version=\"1.0\" encoding=\"UTF-8\" indent=\"yes\"/>\n\n  <!-- template to copy elements -->\n    <xsl:template match=\"*\">\n<xsl:if test=\"normalize-space(string(.)) != ''\">\n        <xsl:element name=\"{local-name()}\">\n            <xsl:apply-templates select=\"@* | node()\"/>\n        </xsl:element>\n</xsl:if>\n    </xsl:template>\n\n    <!-- template to copy attributes -->\n    <xsl:template match=\"@*\">\n        <xsl:attribute name=\"{local-name()}\">\n            <xsl:value-of select=\".\"/>\n        </xsl:attribute>\n    </xsl:template>\n\n    <!-- template to copy the rest of the nodes -->\n    <xsl:template match=\"comment() | text() | processing-instruction()\">\n        <xsl:copy/>\n    </xsl:template>\n\n  <xsl:template match=\"soapenv:*\">\n    <xsl:apply-templates select=\"@* | node()\" />\n  </xsl:template>\n\n  <xsl:template match=\"@xsi:nil[.='true']\"/>\n</xsl:stylesheet>",
    "parameters": [
        {
            "name": "my-parameter",
            "value": "my-value"
        }
    ]
}

{% endcode %}

By default, a DOCTYPE declaration will cause an error. This is for security. If you want to allow it, you can set policy.xslt.secure-processing to false in the Gateway configuration file (gravitee.yml):

policy:
  xslt:
    secure-processing: false

Phases

The phases checked below are supported by the xslt policy:

v2 PhasesCompatible?v4 PhasesCompatible?
onRequesttrueonRequesttrue
onResponsetrueonResponsetrue
onRequestContentfalseonMessageRequestfalse
onResponseContentfalseonMessageResponsefalse

Options

The xslt policy can be configured with the following options:

PropertyRequiredDescriptionTypeDefault
scopetrueExecution scope (request or response)stringRESPONSE
stylesheettrueXSLT stylesheet to applystring
parametersfalseParameters to inject while running XSL transformationArray of XSLT parameters-

Compatibility matrix

The following is the compatibility matrix for APIM and the xslt policy:

Plugin VersionSupported APIM versions
2.x3.x
3.x4.0+

Errors

HTTP status codeMessage
500Bad stylesheet file or XSLT transformation cannot be executed properly