description |
---|
This page provides the technical details of the XSLT policy |
{% hint style="warning" %} This feature requires Gravitee's Enterprise Edition. {% endhint %}
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:
{% hint style="warning" %} This policy can be applied to v2 APIs and v4 HTTP proxy APIs. It cannot be applied to v4 message APIs or v4 TCP proxy APIs. {% endhint %}
{% tabs %} {% tab title="HTTP 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 %}
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
The phases checked below are supported by the xslt
policy:
v2 Phases | Compatible? | v4 Phases | Compatible? |
---|---|---|---|
onRequest | true | onRequest | true |
onResponse | true | onResponse | true |
onRequestContent | false | onMessageRequest | false |
onResponseContent | false | onMessageResponse | false |
The xslt
policy can be configured with the following options:
Property | Required | Description | Type | Default |
---|---|---|---|---|
scope | true | Execution scope (request or response ) | string | RESPONSE |
stylesheet | true | XSLT stylesheet to apply | string | |
parameters | false | Parameters to inject while running XSL transformation | Array of XSLT parameters | - |
The following is the compatibility matrix for APIM and the xslt
policy:
Plugin Version | Supported APIM versions |
---|---|
2.x | 3.x |
3.x | 4.0+ |
HTTP status code | Message |
---|---|
500 | Bad stylesheet file or XSLT transformation cannot be executed properly |