Tuesday, February 2, 2016

Xml to HTML Transformation using XSLT Transform in Mulesoft

Its pretty easy to transform Xml to HTML using Mulesoft's XSLT transform ..

Following is snapshot from my mule's config file ..















The Java component is used to provide following xml as an input :

<?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>
</catalog>
Following is the xsl code :
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />
    <xsl:template match="catalog">
        <html>
            <body>
                <h2>My Book Collection</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Book</th>
                        <th>Genre</th>
                    </tr>
                    <xsl:for-each select="book">
                        <tr>
                            <td>
                                <xsl:value-of select="title" />
                            </td>
                            <td>
                                <xsl:value-of select="genre" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

We can notice that the output method is selected as html.











Once the flow is executed I can see the final html file being created in the output folder. 
I am using file:outbound-endpoint to write the contents in the output file ..



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>