Monday, February 1, 2016

Xml to CSV Transformation using XSLT Transform in Mulesoft

I came across few questions on the internet on how to transform XML to CSV , HTML etc effectively in Mulesoft ..... So thought of trying out the Mule XSLT Transform .. It seems pretty cool and easy to use ... there could be some other better / effective ways but this also doesn't seem that bad ..

I downloaded pre existing xml (call me lazy ;)) from msdn and passed as an input to my xslt transform.  Xml snippet is as below .. its nothing but catalog of books:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">

As you can see in below snapshot .. I am pretty much using Java component (one can use some other component as well ..) to pass on the xml as an input.






My expected output is something like:

Author,Title,Genre,Price,
Gambardella Matthew,XML Developer's Guide,Computer,44.95,

So the magic happens as shown below.... the following is the code snippet from my xslt tranformer

<mulexml:xslt-transformer
maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"

xsl-file="/Users/charlesparkhi/AnypointStudio/connectorws/xslt-usage/src/main/resources/XmlToCsv.xsl" />

Contents of XmlToCsv.xsl are :

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='text' />
<xsl:template match="catalog">
Author,Title,Genre,Price,
<xsl:text></xsl:text>
<xsl:for-each select="book">
<xsl:value-of select="author" />,<xsl:value-of select="title" />,<xsl:value-of select="genre" />,<xsl:value-of select="price" />,
<xsl:text></xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

I m then using file:outbound-endpoint to write the contents of final csv to a file ... named output.csv

<file:outbound-endpoint path="/MyPath/src/main/resources/output" outputPattern="output.csv" connector-ref="File" responseTimeout="10000" doc:name="File"/>

And Wallaah ... The csv is created :









Once opened I can see contents have been accurately represented ..




















Below is the code for this transformation :


<http:listener-config name="HTTP_Listener_Configuration"
host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />
    <file:connector name="File" writeToDirectory="/Users/charlesparkhi/AnypointStudio/connectorws/xslt-usage/src/main/resources/output" autoDelete="false" outputAppend="true" streaming="false" validateConnections="true" doc:name="File"/>
<flow name="xslt-usageFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/" doc:name="HTTP" />
<component class="com.test.xslttransform.InputProvider"
doc:name="Java" />
<mulexml:xslt-transformer
maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"
xsl-file="/Users/charlesparkhi/AnypointStudio/connectorws/xslt-usage/src/main/resources/XmlToCsv.xsl" />
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger level="INFO" message="Final CSV #[payload]" doc:name="Logger"/>
        <file:outbound-endpoint path="/Users/charlesparkhi/AnypointStudio/connectorws/xslt-usage/src/main/resources/output" outputPattern="output.csv" connector-ref="File" responseTimeout="10000" doc:name="File"/>
<set-payload value="/" doc:name="Set Payload"/>
</flow>


11 comments :

  1. Hi,
    I want to call a soap WSDL and using that ouput XML, I want to generate a csv file. I have used web consumer and I am getting the output XML. Now can I directly send this to the XSLT transformer? or any other transformations needed?

    ReplyDelete
  2. the blog is about Xml to CSV Transformation using XSLT Transform in Mulesoft in Mulesoft it is useful for students and Mulesoft Developers for more updates on Mulesoft follow the link

    mulesoft Online Training

    For more info on other technologies go with below links

    Python Online Training

    tableau online training hyderabad

    ServiceNow Online Training

    ReplyDelete
  3. Really the post is very unique.every concepts are captured nice.Thank you for sharing any good knowledge and thanks for fantastic efforts.The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.

    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    oracle online training

    hadoop training in chennai

    hadoop training in bangalore

    ReplyDelete

  4. This information is really awesome thanks for sharing most valuable information.
    Mulesoft Online Training
    Mulesoft Online Training in Hyderabad

    ReplyDelete