Xslt in mule
-
Upload
shahid-shaik -
Category
Technology
-
view
92 -
download
2
Transcript of Xslt in mule
![Page 1: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/1.jpg)
Mule XSLT
![Page 2: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/2.jpg)
We often use XSLT in our application to transform XML payload from one form to another . Mule also supports XSLT in it’s application with XSLT-Transformer component.
![Page 3: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/3.jpg)
So, How can we use XSLT in Mule application??
.
![Page 4: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/4.jpg)
Here I will show you how ……
![Page 5: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/5.jpg)
Before we start we must see what an XSLT actually is :-
As per XSLT definition :-XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or into XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF PostScript and PNG.
Source :- http://en.wikipedia.org/wiki/XSLT
![Page 6: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/6.jpg)
So, XSLT can be use to transform one form of XML to another :-
Here you can see the XSLT is transforming the XML payload into another XML
![Page 7: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/7.jpg)
So, let us try this example in our Mule application
Here we will be using XSLT for transforming the XML payload into another XML like below :-
![Page 8: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/8.jpg)
So let’s consider we have a following flow in our Mule application:-
As you can see we have used a Http inbound end point followed by a set payload component and then XSLT transformer.
Here the set payload contain the XML that need to be transform
![Page 9: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/9.jpg)
So set payload component contains the following payload :-
![Page 10: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/10.jpg)
Our corresponding Mule flow will be as follows :- <flow name="xsltFlow1" doc:name="xsltFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="test" doc:name="HTTP"/> <set-payload value="<RootElement><Name>Anirban</Name><Department>ATS</Department><Designation>SSE</Designation></RootElement>" doc:name="Set Payload"/> <mulexml:xslt-transformermaxIdleTransformers="2" maxActiveTransformers="5" outputEncoding="UTF-8"doc:name="Transform from outer to inner" xsl-file="Transform.xslt"encoding="UTF-8" returnClass="java.lang.String" /> </flow>
As you can see we are using XSLT file :- Transform.xslt
![Page 11: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/11.jpg)
So, the file Transform.xsltshould be in our src/main/resource folder
![Page 12: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/12.jpg)
The file Transform.xsltContains following code to transform :-
<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/"><NewRootElement><NewName><xsl:value-of select="RootElement/Name" /></NewName><NewDepartment><xsl:value-of select="RootElement/Department" /></NewDepartment><NewDesignation><xsl:value-of select="RootElement/Designation" /></NewDesignation></NewRootElement></xsl:template></xsl:stylesheet>
![Page 13: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/13.jpg)
To test the application we hit the url http://localhost:8081/test in our browser and we get the following :-
And you can see the XML payload has been transferred to this new XML
![Page 14: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/14.jpg)
Hope you enjoyed the simple yet an amazing trick in Mule
![Page 15: Xslt in mule](https://reader036.fdocuments.us/reader036/viewer/2022083110/58f2c8c81a28abaa168b45e9/html5/thumbnails/15.jpg)
Thank You