The following example shows how to use the XSLT Transforrmer in Mule ESB. We’ll transform an XML in the payload to another with a different structure.
For this example we’ll use the XSLT transformation described here.
All we need is an input XML like this:
Reservation.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="UTF-8"?> <Reservation> <reservation id="123456789" /> <arrivingDate>03-09-2012</arrivingDate> <departureDate>03-15-2012</departureDate> <guestName>Fred</guestName> <guestLastName>Davis</guestLastName> <hotelID>03</hotelID> <room>1001</room> <oceanView>true</oceanView> <smokingArea>false</smokingArea> </Reservation> |
We want to transform it into an output like this:
ReservationInfo.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <ReservationInfo> <reservation id="123456789" arrivalDate="03-09-2012" departureDate="03-15-2012" /> <guest> <name>Fred</name> <lastName>Davis</lastName> </guest> <room number="1001"> <oceanView>true</oceanView> <smokingArea>false</smokingArea> </room> </ReservationInfo> |
It’s the same data with different structure.
To get this, we need to run an XSL transformation in Mule ESB using the XSLT Transformer.
xsltTransformation-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:ajax="http://www.mulesoft.org/schema/mule/ajax" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="CE-3.3.0" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/ajax http://www.mulesoft.org/schema/mule/ajax/current/mule-ajax.xsd "> <file:connector name="FileConnector" autoDelete="true" streaming="false" validateConnections="true" doc:name="File"> <file:expression-filename-parser /> </file:connector> <flow name="xsltTransformation-configFlow1" doc:name="xsltTransformation-configFlow1"> <file:inbound-endpoint responseTimeout="10000" doc:name="FileInput" connector-ref="FileConnector" path="./file-input-directory"> <file:filename-wildcard-filter pattern="*.xml" /> </file:inbound-endpoint> <logger message="#[payload]" level="INFO" doc:name="Logger"/> <mulexml:xslt-transformer maxIdleTransformers="2" maxActiveTransformers="5" xsl-file="src/main/resources/xslt/transform.xsl" doc:name="XSLT" /> <file:outbound-endpoint path="./file-output-directory" responseTimeout="10000" doc:name="FileOutput" outputPattern="#[function:datestamp]-#[header:originalFilename]" /> </flow> </mule> |
We need to tell the XSLT Transformer where the XSL transformation is located in our project (line 26).
Transformation.xslt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <xsl:template match="/"> <ReservationInfo> <reservation> <xsl:attribute name="id"> <xsl:value-of select='Reservation/reservation/@id' /> </xsl:attribute> <xsl:attribute name="arrivalDate"> <xsl:value-of select='Reservation/arrivingDate' /> </xsl:attribute> <xsl:attribute name="departureDate"> <xsl:value-of select='Reservation/departureDate' /> </xsl:attribute> </reservation> <guest> <name> <xsl:value-of select="Reservation/guestName" /> </name> <lastName> <xsl:value-of select="Reservation/guestLastName" /> </lastName> </guest> <room> <xsl:attribute name="number"> <xsl:value-of select='Reservation/room' /> </xsl:attribute> <oceanView> <xsl:value-of select="Reservation/oceanView" /> </oceanView> <smokingArea> <xsl:value-of select="Reservation/smokingArea" /> </smokingArea> </room> </ReservationInfo> </xsl:template> </xsl:stylesheet> |
You might get a warning in Mule’s console, telling you that you are running an XSLT 1.0 in an XSLT 2.0 processor, but its OK, the transformation will work.
You can find the project in Github here.
Here is the source code of the project.
Enjoy!
6 comments: On Using the XSLT Transformer in Mule ESB
Hi
I am trying to use something similar to add an element to incoming message. But in doing so, mule is adding namespaces to the copied element.
Following is my transformer code:
which adds a uuid to the request element as follows:
?xml version=”1.0″ encoding=”UTF-8″?>
12345
CallerId
SoapUI
CallerId
1.0
1.0
SOAPUI
bff3e1d6-ecdd-41ae-8807-ec04085a2b54
But it also adds the namespace for mule & uuid.
Any idea what could be a work around??
/A
I am using the XSLT transformer to transform XML, to XML2 – there are no errors but the transformed xml is not having any xml tags but only the data values.
Is there any configuration missing ?
I have noticed you don’t monetize your page, don’t waste your traffic, you can earn extra bucks every month because you’ve got hi quality content.
If you want to know how to make extra bucks, search
for: best adsense alternative Wrastain’s tools
I have noticed you don’t monetize your page, don’t waste
your traffic, you can earn additional cash every
month because you’ve got high quality content. If you
want to know how to make extra $$$, search for: Ercannou’s essential tools
best adsense alternative
I will be thankful for the service and then trust you find out what a powerful job you were undertaking instructing the others thru your webpage.
I like this web blog very much, Its a really nice position to read and incur information. Griselda Dennie Savage