The XSLT Mediator applies a specified XSLT transformation to a selected element of the current message payload. In addition, you can:
- Specify properties already included in the mediation flow to be added to the XSLT script as XSLT parameters.
- Specify features to be enabled/disabled in the XSLT transformation.
- Import external XSLT scripts to the main XSLT script of the XSLT mediator by adding them as resources.
Syntax
<xslt key="string" [source="xpath"]> <property name="string" (value="literal" | expression="xpath")/>* <feature name="string" value="true| false" />* <resource location="string" key="string"/>* </xslt>
UI Configuration
Parameter Name | Description |
---|---|
Key Type | You can select one of the following options.
|
Key | This specifies the registry key to refer the XSLT to. This supports static and dynamic keys. |
Source | This determines the element to which the given XSLT transformation should be applied via an XPath expression. If the source element is not specified, the XSLT transformation is applied to the first child of the SOAP body. Tip You can click NameSpaces to add namespaces if you are providing an expression. Then the Namespace Editor panel would appear where you can provide any number of namespace prefixes and URLs used in the XPath expression. |
Properties of the XSLT mediator | This section is used to inject properties set in the mediation flow to the XSLT script as XSLT parameters. These are referred from the XSLT in transformation using the Parameters relating to the properties are as follows.
|
Features of the XSLT mediator | This section is used to specify features to be enabled/disabled in the XSLT transformation. For example, adding the Parameters relating to the features are as follows.
|
Resources of the XSLT mediator | This section is used to import external XSLT scripts to the main XSLT scripts defined in the XSLT mediator. The XSLT scripts to be imported are first added as resources in the registry. Parameters relating to the resources are as follows.
|
Note
You can also configure the Mediator using XML. Click switch to source view in the Mediator window.
Examples
Example 1 - Applying a XSLT transformation to a element selected based on an XPath expression
In this example, the XSLT can be picked by the key transform/example.xslt
and the XSLT would be applied to a part of the message that is specified as an XPath expression. In this case, it is applied to s11:Body/child
the message.
<xslt xmlns="http://ws.apache.org/ns/synapse" key="transform/example.xslt" source="s11:Body/child" />
Example 2 - Adding properties as XSLT parameters
In this example, a property named PARAM_NAME
is added to the XSLT script as an XSLT parameter. A XPath expression is used to assign this property the value of the ORDER_ID
property in the default scope.
<xslt key="keyToXSLTFile"> <property expression="$ctx:ORDER_ID" name="PARAM_NAME"> </property></xslt>
The XSLT script with the PARAM_NAME
property added would look as follows.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="PARAM_NAME"></xsl:param> <xsl:template match="/"> <orders xmlns="http://services.samples"> <xsl:attribute name="id"> <xsl:value-of select="$PARAM_NAME"> </xsl:value-of></xsl:attribute> </orders> </xsl:template> </xsl:stylesheet>
Example 3 - Adding XSLT imports as resources
In this example, two XSLT files saved in the registry under conf:/
as resources are imported to the main XSLT script of the XSLT mediator.
xslt1.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="//people/person" name="FILL_PPL"> <client> <firstname> <xsl:value-of select="firstname"> </xsl:value-of></firstname> <lastname> <xsl:value-of select="lastname"> </xsl:value-of></lastname> <age> <xsl:value-of select="age"> </xsl:value-of></age> <country> <xsl:value-of select="country"> </xsl:value-of></country> </client> </xsl:template> </xsl:stylesheet>
xslt2.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:include href="xslt1.xslt"> <xsl:template match="/"> <clients> <xsl:for-each select="//people/person"> <xsl:call-template name="FILL_PPL"> </xsl:call-template></xsl:for-each> </clients> </xsl:template> </xsl:include></xsl:stylesheet>
<xsl:include href="xslt1.xslt"> element indicates that the xslt1.xslt
is included in xslt2.xslt
.
These two files can be imported to the script of the XSLT mediator as follows.
<xslt key="conf:/xslt2.xslt"> <resource key="conf:/xslt1.xslt" location="xslt1.xslt"> </resource></xslt>
The following SOAP request can be used to test the above configuration of the XSLT mediator included in a proxy configuration.
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:header> <soapenv:body> <people> <person> <firstname>Isuru</firstname> <lastname>Udana</lastname> <gender>Male</gender> <age>26</age> <country>SriLanka</country> </person> <person> <firstname>Ishan</firstname> <lastname>Jayawardena</lastname> <gender>Male</gender> <age>26</age> <country>SriLanka</country> </person> </people> </soapenv:body> </soapenv:header></soapenv:envelope>