How to Transform XML using XSLT in WSO2 Enterprise Integrator
The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). XSLT uses stylesheets to convert XML into other data formats. You can convert part or all of an XML document and select or rearrange the data using the XPath query language.
XSLT has two main areas of applicability:
- Formatting (conversion of XML into HTML)
- Data exchange (querying, reorganizing and converting data from one XML schema to another, or into a data exchange format such as SOAP)
WSO2 Enterprise Integrator (WSO2 ESB) is an open-source hybrid integration platform that enables API-centric integration using integration architecture styles such as microservices or centralized ESB. The platform provides a graphical drag-and-drop flow designer and a configuration-driven approach to build low-code integration solutions for cloud- and container-native projects.
WSO2 Enterprise Integrator supports XSLT Transformation via XSLT Mediator. The XSLT Mediator applies a specified XSLT transformation to a selected element of the current message payload.
Tutorial to XSLT Transformation Using WSO2 Enterprise Integrator
Let’s assume that we have the following XML as the input
<product pid="100-201-01">
<description>
<name>Ice Scraper, Windshield 4 inch</name>
<details>Basic Ice Scraper 4 inches wide, foam handle</details>
<price>3.99</price>
</description>
</product>
We need to transform the above XML to the following XML file to be compatible with HTML browser
<html>
<body>
<h1>Ice Scraper, Windshield 4 inch</h1>
<table border="1">
<th>
<tr>
<td width="80">product ID</td><td>100-201-01</td>
</tr>
<tr>
<td width="200">product name</td><td>Ice Scraper, Windshield 4 inch</td>
</tr>
<tr>
<td width="200">price</td><td>$3.99</td>
</tr>
<tr>
<td width="50">details</td><td>Basic Ice Scraper 4 inches wide, foam handle</td>
</tr>
</th>
</table>
</body>
</html>
For the above transformation, we are going to use the following XLST file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1><xsl:value-of select="/product/description/name"/></h1>
<table border="1">
<th><xsl:apply-templates select="product"/></th>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="product">
<tr>
<td width="80">product ID</td>
<td><xsl:value-of select="@pid"/></td>
</tr>
<tr>
<td width="200">product name</td>
<td><xsl:value-of select="/product/description/name"/></td>
</tr>
<tr>
<td width="200">price</td>
<td>$<xsl:value-of select="/product/description/price"/></td>
</tr>
<tr>
<td width="50">details</td>
<td><xsl:value-of select="/product/description/details"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
Also, you can learn more about writing your XSLT files using the Online tutorials(https://www.w3schools.com/xml/xsl_examples.asp)
Now, let’s Start the implementation using WSO2 Integration Studio(you can download the tool from https://wso2.com/integration/integration-studio/). First, start a new integration solution project using the Help->Getting Started dashboard.
In the new project creation wizard make sure to select “Create Registry Resource Project”. This registry project is required for storing the XSLT file.
Then, add a new Registry Resource by right-clicking on the RegistryResource Project.
After clicking on the Registry Resource Item, Select “From existing template” and proceed to the next step, then we have to give a name to the registry resource and select XSLT from the list of templates.
Now open the XSLT resource file created under the Registry Resource Project. Then paste the above XSLT content to the registry resource.
After completing creating the XSLT Registry Resource, we can start to develop the simple proxy service that will do the XSLT transformation.
Let’s add a Proxy service to the ESB Config Project. First, create a new Proxy service by right-clicking the ESB config project. Then add a Log mediator with FULL Log Level. Then add an XSLT Mediator and Configure its Static Schema Key to the created XSLT resource by clicking on the ‘workspace’ in the Registry Resource selector dialog. Then, add another Log mediator to see the transformed message in the console logs. Finally, add a Respond Mediator to send back the request to the client.
<?xml version="1.0" encoding="UTF-8"?><proxy name="XSLTTransformProxy" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse"><target><inSequence><log description="Log before Transformation" level="full"><property name="*****Message*****" value="*****Before the XSLT Transformation********"/></log><xslt description="XSLT Trnasformation" key="conf:XSLTResources/XSLTResourceFile.xslt"/><log description="Log after the Transformation" level="full"><property name="*****Message******" value="*****After Transforming the Message Using XSLT******"/></log><respond description="Respond back to client"/></inSequence><outSequence/><faultSequence/></target></proxy>
Now we have completed the Tutorial with the XSLT Mediator. We can run the proxy service using the Integration Studio and use the service to transform the request. To run the selected proxy, right-click on the Composite application project and select “Export artifacts and run”.
You can invoke the proxy service athttp://localhost:8290/services/XSLTTransformProxy
with the above XML Payload using HTTP client like Postman or SOAPUI
We have successfully completed the XSLT Transformation tutorial with WSO2 Integration Studio.
Thank You. !!!!!!
References:
[1]https://wso2.com/integration/integration-studio/
[2]https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/rzasp/rzaspxml0648.htm
[3]https://ei.docs.wso2.com/en/latest/micro-integrator/references/mediators/xSLT-Mediator/