XML stylesheet language transformations (XSLT) is a language for converting documents from one XML format to another XML format. It is often used to integrate applications that support XML, but that require different XML-formats for the same data.
The API Platform relies on the Saxon XSLT processor, and supports XSLT 1.0 and 2.0.
The XSL policy type enables XSLT to execute as a processing step in an API proxy flow. The XSLT is implemented in a stand-alone.xslt file, which is stored in the API proxy under /resources/xsl. The XSL policy merely references the XSL file.
The XSL policy requires two inputs:
- The name of an XSLT stylesheet, which contains a set of transformation rules) stored in the API proxy under
/resources/xsl - The source of the XML to be transformed (typically a request or response message)
Sample
A simple XSL policy is:
<XSL name="TransformXML">
<ResourceURL>xsl://{myfile}.xsl</ResourceURL>
<Source>request</Source>
</XSL>
There are many tools and resources on the Internet implementing XSLT. For reference, below are some XSLT examples with sample request messages and transformed messages.
XSLT file
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:text><Life></xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text>Here are the odd-numbered items from the list:</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:for-each select="list/listitem">
<xsl:if test="(position() mod 2) = 1">
<xsl:number format="1. "/>
<xsl:value-of select="."/>
<xsl:value-of select="$newline"/>
</xsl:if>
</xsl:for-each>
<xsl:text></Life></xsl:text>
</xsl:template>
</xsl:stylesheet>
Request Message
<?xml version="1.0"?> <list> <title>A few of my favorite albums</title> <listitem>A Love Supreme</listitem> <listitem>Beat Crazy</listitem> <listitem>Here Come the Warm Jets</listitem> <listitem>Kind of Blue</listitem> <listitem>London Calling</listitem> <listitem>Remain in Light</listitem> <listitem>The Joshua Tree</listitem> <listitem>The Indestructible Beat of Soweto</listitem> </list>
Transformed message
<Life> Here are the odd-numbered items from the list: 1. A Love Supreme 3. Here Come the Warm Jets 5. London Calling 7. The Joshua Tree </Life>
Configuring an XSL Transformation policy
Configure an XSL Transformation policy using the following elements.
Note: <xsl:include> and <xsl:import> are not supported.
| Field Name | Description | |||
|---|---|---|---|---|
| Name (Mandatory) | Name of the policy. | |||
| Source (Optional) | Contains the message from which information needs to be extracted. Usually this value is set to request or response, depending on whether the message to be transformed is inbound or outbound.
|
|||
| OutputVariable (Optional) |
A variable that stores the output of the transformation. Usually, this value is the same as source, either
request or response, since a transformation is typically applied inline.If OutputVariable is not specified, the message content is replaced with the output of the transformation.
|
|||
| ResourceURL (Mandatory) | The XSLT file to be used for transforming the message. | |||
| Parameters (Optional) | ignoreUnresolvedVariables (Optional) |
Ignores any unresolved variable errors in the XSLT script instructions.
Valid values: true/false
Default value: false
|
||
| Parameter (Optional) | name (Mandatory) | Name of a custom parameter. Note that with name you can only use one of the optional parameters listed below. | ||
| ref (Optional) |
Specifies the reference that sources the value from a variable.
In the XSL example below, if request.header.greeting is null, ignoreUnresolvedVariables responds in one of two ways:
|
|||
| value (Optional) | Value of the parameter. | |||
Policy schema
Each policy must conform to a policy schema. All policy constructs such as elements and attributes mentioned above are defined in a schema. To download the schema, click here.