<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>redstream &#187; MapForce</title>
	<atom:link href="http://www.redstream.nl/tag/mapforce/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.redstream.nl</link>
	<description>Pragmatic Integrators</description>
	<lastBuildDate>Fri, 27 Jan 2012 19:31:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Writing your own Altova MapForce Extension</title>
		<link>http://www.redstream.nl/2011/07/05/writing-your-own-altova-mapforce-extension/</link>
		<comments>http://www.redstream.nl/2011/07/05/writing-your-own-altova-mapforce-extension/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 20:24:00 +0000</pubDate>
		<dc:creator>Pascal Prins</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[MapForce]]></category>

		<guid isPermaLink="false">http://www.redstream.nl/?p=2703</guid>
		<description><![CDATA[In a previous post, I mentioned that you could extend the function library in Altova MapForce and add your own functions. In this post I&#8217;ll show you (briefly) how to do this using the Java methods from the previous post. &#8230; <a href="http://www.redstream.nl/2011/07/05/writing-your-own-altova-mapforce-extension/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.redstream.nl%2F2011%2F07%2F05%2Fwriting-your-own-altova-mapforce-extension%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2011%2F07%2F05%2Fwriting-your-own-altova-mapforce-extension%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In a <a href="http://www.redstream.nl/2011/03/29/xslt-2-0-and-java-extensions/">previous</a> post, I mentioned that you could extend the function library in Altova MapForce and add your own functions. In this post I&#8217;ll show you (briefly) how to do this using the Java methods from the previous post.</p>
<p><span id="more-2703"></span></p>
<p>The MapForce documentation only contains a little information on how to add your own custom functions. </p>
<p>The base directory in which you installed MapForce contains a directory called <code>MapForceLibraries</code>. Here, you will find a number of, so called, <code>mff</code> files, which stands for MapForce Functions. Inside, the definition is stored of the functions you are able to select from the library when working on a mapping. We will be using this principle to add an extension of our own. The only thing we need to do, is create such a <code>mff</code> file. </p>
<p>As I already said, the MapForce documentation does not contain a lot of information on this subject. But &#8230; what it does include is the XML Schema for the <code>mff</code> file. It is called <code>mff.xsd</code> and it is also located in the <code>MapForceLibraries</code> directory. Open the XML Schema in XMLSpy (or any other XML editor) and select element <code>mapping</code> to get an overview of the library structure. Notice that the XML Schema is well-documented (at least better than the actual MapForce documentation). </p>
<p>We will, briefly, walk through the most important elements. First, element <code>mapping</code>: the root element. </p>
<p><a href="http://www.redstream.nl/wp-content/uploads/2011/05/mapforce-mff-mapping.png"><img src="http://www.redstream.nl/wp-content/uploads/2011/05/mapforce-mff-mapping.png" alt="" title="mapforce-mff-mapping" width="338" height="227" class="alignnone size-full wp-image-2709" /></a><br/></p>
<p>First, element <code>implementations</code>. This element refers to the possible implementations of your extension (e.g. Java, XSLT, C#, etc).</p>
<p><a href="http://www.redstream.nl/wp-content/uploads/2011/05/mapforce-mff-implementations.png"><img src="http://www.redstream.nl/wp-content/uploads/2011/05/mapforce-mff-implementations.png" alt="" title="mapforce-mff-implementations" width="627" height="253" class="alignnone size-full wp-image-2708" /></a></p>
<p>Functions can be grouped, hence the <code>group</code> element. The <code>component</code> element inside describes how your extension should be called. The <code>target</code> describe your input parameters (if any) and <code>output</code> describes your output.</p>
<p><a href="http://www.redstream.nl/wp-content/uploads/2011/05/mapforce-mff-group.png"><img src="http://www.redstream.nl/wp-content/uploads/2011/05/mapforce-mff-group.png" alt="" title="mapforce-mff-group" width="499" height="427" class="alignnone size-full wp-image-2706" /></a></p>
<p>Here&#8217;s what an example XML could look like. We used an <code>xslt2</code> implementation that points to a xslt stylesheet.</p>

<div class="wp_codebox"><table><tr id="p27034"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p2703code4"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mapping</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xsi:noNamespaceSchemaLocation</span>=<span style="color: #ff0000;">&quot;file:///C:/Program%20Files/Altova/MapForce2011/MapForceLibraries/mff.xsd&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;8&quot;</span> <span style="color: #000066;">library</span>=<span style="color: #ff0000;">&quot;redstream&quot;</span> <span style="color: #000066;">xmlns:xs</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span> <span style="color: #000066;">xmlns:mf</span>=<span style="color: #ff0000;">&quot;http://www.altova.com/MapForce/2010/standard&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;implementations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;implementation</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;xslt2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;setting</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;xsltfilename&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;xlst-redstream-library.xsl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/implementation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/implementations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;group</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;demo functions&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;component</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;greeting&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;datapoint</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xs:string&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;targets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;datapoint</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;result&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xs:string&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/targets<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;implementations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;implementation</span> <span style="color: #000066;">language</span>=<span style="color: #ff0000;">&quot;xslt2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;function</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rs:greeting&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;java:nl.redstream.XsltUtils&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/implementation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/implementations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;short<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>result = 'Hello ' + name + ' !'<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/short<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;long<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Returns friendly message.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/long<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/component<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/group<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The related XSLT file looks like this:</p>

<div class="wp_codebox"><table><tr id="p27035"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p2703code5"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> </span>
<span style="color: #009900;">   <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> </span>
<span style="color: #009900;">   <span style="color: #000066;">xmlns:xs</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span> </span>
<span style="color: #009900;">   <span style="color: #000066;">xmlns:fn</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2005/xpath-functions&quot;</span> </span>
<span style="color: #009900;">   <span style="color: #000066;">xmlns:rs</span>=<span style="color: #ff0000;">&quot;java:nl.redstream.utils.XsltUtils&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:function</span> <span style="color: #000066;">as</span>=<span style="color: #ff0000;">&quot;xs:string&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;rs:greeting&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">as</span>=<span style="color: #ff0000;">&quot;xs:string&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;rs:greeting($name)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:function<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Once, you&#8217;ve created your <code>mff</code> and <code>xslt</code> file, copy them into <code>MapForceLibraries</code> directory (make sure MapForce is not running, otherwise you will not be able to write in that directory). Start Altova MapForce, create a new mapping (implemenation <code>xslt2</code> and behold &#8230; your extension will show up in the Library. </p>
<p><a href="http://www.redstream.nl/wp-content/uploads/2011/05/mapforce-custom-library.png"><img src="http://www.redstream.nl/wp-content/uploads/2011/05/mapforce-custom-library.png" alt="" title="mapforce-custom-library" width="286" height="165" class="alignnone size-full wp-image-2729" /></a></p>
<p>Now, you can drag-and-drop the function onto your mapping.</p>
<p>The resulting XSLT will look like this:</p>

<div class="wp_codebox"><table><tr id="p27036"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p2703code6"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">This file was generated by Altova MapForce 2011sp1</span>
&nbsp;
<span style="color: #808080; font-style: italic;">YOU SHOULD NOT MODIFY THIS FILE, BECAUSE IT WILL BE</span>
<span style="color: #808080; font-style: italic;">OVERWRITTEN WHEN YOU RE-RUN CODE GENERATION.</span>
&nbsp;
<span style="color: #808080; font-style: italic;">Refer to the Altova MapForce Documentation for further details.</span>
<span style="color: #808080; font-style: italic;">http://www.altova.com/mapforce</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> </span>
<span style="color: #009900;">                                              <span style="color: #000066;">xmlns:xs</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span> </span>
<span style="color: #009900;">                                              <span style="color: #000066;">xmlns:fn</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2005/xpath-functions&quot;</span> </span>
<span style="color: #009900;">                                              <span style="color: #000066;">xmlns:rs</span>=<span style="color: #ff0000;">&quot;java:nl.redstream.XsltUtils&quot;</span> <span style="color: #000066;">exclude-result-prefixes</span>=<span style="color: #ff0000;">&quot;xs fn rs&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:output</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;xml&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000066;">indent</span>=<span style="color: #ff0000;">&quot;yes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dataset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;row<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;dataset/row/column&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;rs:greeting(fn:string(@name))&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/column<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/row<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dataset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Unfortunately, you cannot display the results of the customized mapping in MapForce (#featurerequest). Instead, you could use XMLSpy and use the settings from the <a href="http://www.redstream.nl/2011/03/29/xslt-2-0-and-java-extensions/">previous</a> post.</p>
<p>That&#8217;s it &#8230; enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2011/07/05/writing-your-own-altova-mapforce-extension/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using parameters with XSLT in Mule</title>
		<link>http://www.redstream.nl/2011/03/04/using-parameters-with-xslt-in-mule/</link>
		<comments>http://www.redstream.nl/2011/03/04/using-parameters-with-xslt-in-mule/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 11:31:18 +0000</pubDate>
		<dc:creator>Pascal Alma</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[MapForce]]></category>
		<category><![CDATA[Mule2]]></category>
		<category><![CDATA[XML/ XSD/ XSLT]]></category>
		<category><![CDATA[Mule]]></category>
		<category><![CDATA[XML/XSD/XSLT]]></category>

		<guid isPermaLink="false">http://www.redstream.nl/?p=2564</guid>
		<description><![CDATA[Recently I got into a situation where I had to put &#8216;metadata&#8217; values (which were part of the Mule Message) into the XML result of an XSLT transformer. Let me explain the situation with an example. Imagine you have an &#8230; <a href="http://www.redstream.nl/2011/03/04/using-parameters-with-xslt-in-mule/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.redstream.nl%2F2011%2F03%2F04%2Fusing-parameters-with-xslt-in-mule%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2011%2F03%2F04%2Fusing-parameters-with-xslt-in-mule%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Recently I got into a situation where I had to put &#8216;metadata&#8217; values (which were part of the Mule Message) into the XML result of an XSLT transformer. Let me explain the situation with an example. Imagine you have an &#8216;order&#8217; xml that needs to be translated into an &#8216;invoice&#8217; xml. Here is a simple &#8216;order.xsd&#8217;:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2011/03/order-xsd.png"><img src="http://www.redstream.nl/wp-content/uploads/2011/03/order-xsd.png" alt="" title="order-xsd" width="355" height="126" class="aligncenter size-full wp-image-2568" /></a></p>
<p>Here is the &#8216;invoice.xsd&#8217;:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2011/03/invoice-xsd.png"><img src="http://www.redstream.nl/wp-content/uploads/2011/03/invoice-xsd-300x106.png" alt="" title="invoice-xsd" class="aligncenter size-medium wp-image-2569" /></a></p>
<p>The mapping for the transformation looks like this in MapForce:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2011/03/mapping-without-param.png"><img src="http://www.redstream.nl/wp-content/uploads/2011/03/mapping-without-param-300x133.png" alt="" title="mapping-without-param" class="aligncenter size-medium wp-image-2570" /></a><span id="more-2564"></span></p>
<p>Now the invoice number that is used in the invoice should be the MuleMessage correlationID&#8230;. (I know, weird situation, but it is only an example). The first thing that came to mind was to create a new transformer that would run after the XSLT transformer. This new transformer would add the element &#8216;invoicenr&#8217;  with the correlationID as value to the payload of the MuleMessage. But it can be done much easier! Here is the modified mapping that I created with MapForce:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2011/03/mapping-with-param.png"><img src="http://www.redstream.nl/wp-content/uploads/2011/03/mapping-with-param-300x288.png" alt="" title="mapping-with-param" class="aligncenter size-medium wp-image-2571" /></a><br />
In MapForce you can add extra input parameters for the XSL mapping as shown above. Now generate the XSLT and use that as basis for your Mule XSLT Transformer. In the Mule config you add the context-property to your XSL Transformer configuration to tell Mule with which value the parameter in the XSLT file should be filled:</p>

<div class="wp_codebox"><table><tr id="p25648"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p2564code8"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mule</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.mulesource.org/schema/mule/core/2.2&quot;</span></span>
<span style="color: #009900;">     <span style="color: #000066;">xmlns:xm</span>=<span style="color: #ff0000;">&quot;http://www.mulesource.org/schema/mule/xml/2.2&quot;</span></span>
<span style="color: #009900;">     ...</span>
<span style="color: #009900;">     <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span>
<span style="color: #009900;">      http://www.mulesource.org/schema/mule/xml/2.2 http://www.mulesource.org/schema/mule/xml/2.2/mule-xml.xsd</span>
<span style="color: #009900;">     ...</span>
<span style="color: #009900;">     &quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xm:xslt-transformer</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;orderToInvoiceTransformer&quot;</span> <span style="color: #000066;">xsl-file</span>=<span style="color: #ff0000;">&quot;xsl/order-to-invoice.xsl&quot;</span> <span style="color: #000066;">outputEncoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xm:context-property</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;invoiceNr&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#[message:correlationId]&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ship:xslt-transformer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This makes it a very clean and powerful way to add metadata to your payload might you ever need this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2011/03/04/using-parameters-with-xslt-in-mule/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reloadable XSLT Transformer in Mule</title>
		<link>http://www.redstream.nl/2010/12/19/reloadable-xslt-transformer-in-mule/</link>
		<comments>http://www.redstream.nl/2010/12/19/reloadable-xslt-transformer-in-mule/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 19:10:00 +0000</pubDate>
		<dc:creator>Pascal Alma</dc:creator>
				<category><![CDATA[Mule2]]></category>
		<category><![CDATA[XML/ XSD/ XSLT]]></category>
		<category><![CDATA[MapForce]]></category>
		<category><![CDATA[XML/XSD/XSLT]]></category>

		<guid isPermaLink="false">http://blog.redstream.nl/?p=2032</guid>
		<description><![CDATA[One of the main functions of a Mule ESB application (besides transporting messages) is transforming messages to another format. Nowadays a lot of messages are XML based so a lot of transformations are done by using XSLT, or in Mule &#8230; <a href="http://www.redstream.nl/2010/12/19/reloadable-xslt-transformer-in-mule/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.redstream.nl%2F2010%2F12%2F19%2Freloadable-xslt-transformer-in-mule%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2010%2F12%2F19%2Freloadable-xslt-transformer-in-mule%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>One of the main functions of a <a href="http://www.mulesoft.org/documentation/display/MULE2USER/Home">Mule ESB</a> application (besides transporting messages) is transforming messages to another format. Nowadays a lot of messages are XML based so a lot of transformations are done by using <a href="http://en.wikipedia.org/wiki/XSLT">XSLT</a>, or in Mule terminology, an <a href="http://www.mulesoft.org/documentation/display/MULE2USER/XSLT+Transformer">XSLT Transformer</a>. In our current project this is no different.</p>
<p>As I posted <a href="http://www.redstream.nl/2010/05/21/introducing-altova-mapforce/">before</a> we happily using <a href="http://www.altova.com/mapforce.html">Altova Mapforce</a> for this. But this also means that a lot of changes and issues during the development are being solved by modifying the XSLT. In our Mule ESB setup we have deployed our Mule applications as a WAR in Tomcat. So this means to take our modified XSLT into account we have to redeploy the WAR or at least, stop and start the WAR in Tomcat. And as we found out, if the Mule application is inside a JAR inside the WAR we had to stop and start the complete Tomcat instance for our changes to be picked up.</p>
<p>To solve this issue we have created our own instance of the Xslt Transformer in Mule. Our implementation checks for the timestamp of the XSLT file and reloads it if there is found a newer XSLT file. This way we don&#8217;t have to restart our Tomcat if a newer version is in place. In a production environment you might want to discard this functionality to avoid unexpected behaviour of the running applications but I leave that up to you. In this post I will show you how our implementation of the XSLT Transformer works. <span id="more-2032"></span><br />
The example I use in this pots is the same as I had in the previous post. I have an XSD that describes an Order and I transform it with a XSL file to an Invoice. The schema for the Order looks like this:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/11/rs-schema.png"><img class="alignnone size-medium wp-image-1994" title="rs-schema" src="http://www.redstream.nl/wp-content/uploads/2010/11/rs-schema-300x135.png" alt="" width="300" height="135" /></a></p>
<p>The schema for the Invoice looks like this:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/11/invoice.png"><img class="alignnone size-medium wp-image-2034" title="invoice" src="http://www.redstream.nl/wp-content/uploads/2010/11/invoice-300x133.png" alt="" width="300" height="133" /></a></p>
<p>The mapping in MapForce looks like this:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/11/mapping.png"><img class="alignnone size-medium wp-image-2033" title="mapping" src="http://www.redstream.nl/wp-content/uploads/2010/11/mapping-300x159.png" alt="" width="300" height="159" /></a></p>
<p>Now for the real thing. <a href="http://www.redstream.nl/wp-content/uploads/2010/11/XsltTransformer.java">Here</a> you can find the complete code  of our version of the XSL Transformer. Next I show just the important parts that we have changed</p>

<div class="wp_codebox"><table><tr id="p203210"><td class="line_numbers"><pre>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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code" id="p2032code10"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">nl.redstream.transformer</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> …
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
* This class is mainly a copy of the XsltTransformer supplied by Mule.
* Checks are added for the XSLT file timestamp, so if the XSLT file is changed
* , the pool will be reloaded, so the new version of the XSLT is used.
*
* @author Redstream
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> XsltTransformer <span style="color: #000000; font-weight: bold;">extends</span> AbstractXmlTransformer <span style="color: #000000; font-weight: bold;">implements</span> MuleContextAware
<span style="color: #009900;">&#123;</span>
...
  <span style="color: #666666; font-style: italic;">/* added by Redstream */</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> lastModified<span style="color: #339933;">;</span>
&nbsp;
...
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> initialise<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> InitialisationException
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">try</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Set XSLT timestamp</span>
      java.<span style="color: #006633;">net</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URL</span></a> url <span style="color: #339933;">=</span> IOUtils.<span style="color: #006633;">getResourceAsUrl</span><span style="color: #009900;">&#40;</span>xslFile, getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a> file <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">try</span>
      <span style="color: #009900;">&#123;</span>
        file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>url.<span style="color: #006633;">toURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>URISyntaxException e<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>url.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>file <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastModified</span> <span style="color: #339933;">=</span> file.<span style="color: #006633;">lastModified</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      transformerPool.<span style="color: #006633;">addObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Athrowable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Throwable</span></a> te<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> InitialisationException<span style="color: #009900;">&#40;</span>te, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
...
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doTransform<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> encoding, Source sourceDoc, Result result<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a>
  <span style="color: #009900;">&#123;</span>
    DefaultErrorListener errorListener <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultErrorListener<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    javax.<span style="color: #006633;">xml</span>.<span style="color: #006633;">transform</span>.<span style="color: #006633;">Transformer</span> transformer <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Check XSLT timestamp and reload</span>
    java.<span style="color: #006633;">net</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URL</span></a> url <span style="color: #339933;">=</span> IOUtils.<span style="color: #006633;">getResourceAsUrl</span><span style="color: #009900;">&#40;</span>xslFile, getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a> file <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">try</span>
    <span style="color: #009900;">&#123;</span>
      file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>url.<span style="color: #006633;">toURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>URISyntaxException e<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afile+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">File</span></a><span style="color: #009900;">&#40;</span>url.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>file <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> file.<span style="color: #006633;">lastModified</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastModified</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>logger.<span style="color: #006633;">isDebugEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        logger.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Reloading &quot;</span> <span style="color: #339933;">+</span> file.<span style="color: #006633;">getAbsolutePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">transformerPool</span>.<span style="color: #006633;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">transformerPool</span>.<span style="color: #006633;">addObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastModified</span> <span style="color: #339933;">=</span> file.<span style="color: #006633;">lastModified</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ...
  <span style="color: #009900;">&#125;</span>
...
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2010/12/19/reloadable-xslt-transformer-in-mule/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Combining Mule with Altova MapForce</title>
		<link>http://www.redstream.nl/2010/06/03/combining-mule-with-altova-mapforce/</link>
		<comments>http://www.redstream.nl/2010/06/03/combining-mule-with-altova-mapforce/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 06:35:11 +0000</pubDate>
		<dc:creator>Pascal Alma</dc:creator>
				<category><![CDATA[MapForce]]></category>
		<category><![CDATA[Mule]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[XML/XSD/XSLT]]></category>

		<guid isPermaLink="false">http://blog.redstream.nl/?p=1753</guid>
		<description><![CDATA[As promised in our previous blog about Altova Mapforce we will show in this post how to combine the result of the generated code of Altova Mapforce with a Mule application. As we have shown before, the Java code that &#8230; <a href="http://www.redstream.nl/2010/06/03/combining-mule-with-altova-mapforce/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.redstream.nl%2F2010%2F06%2F03%2Fcombining-mule-with-altova-mapforce%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2010%2F06%2F03%2Fcombining-mule-with-altova-mapforce%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>As promised in our <a href="http://www.redstream.nl/2010/05/21/introducing-altova-mapforce/">previous</a> blog about <a href="http://www.altova.com/mapforce.html">Altova Mapforce</a> we will show in this post how to combine the result of the generated code of Altova Mapforce with a <a href="http://www.mulesoft.org/">Mule</a> application. As we have shown before, the Java code that is generated by MapForce is divided in two parts:</p>
<ul>
<li>one is Altova generic, so will be reused for each mapping</li>
<li>one is mapping specific so is the actual part that changes with the mapping (this package name is configurable)</li>
</ul>
<p><span id="more-1753"></span></p>
<p>We first jarred the Altova generic code and added it to our Maven repository (<a href="http://www.jfrog.org/index.php">Artifactory</a>) so we can include this in our Mule project as dependency. We removed the generated mapping-specific Java code and used the included Ant build script to create the jar file. Here is a screenshot of how it looks in Artifactory:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/05/Screen-shot-2010-05-28-at-8.15.09-AM.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/05/Screen-shot-2010-05-28-at-8.15.09-AM-300x187.png" alt="" title="ArtifactoryScreenshot" width="300" height="187" class="alignnone size-medium wp-image-1803" /></a></p>
<p>Now if we want to use Mapforce generated code, we add the following dependency to the pom:</p>

<div class="wp_codebox"><table><tr id="p175313"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1753code13"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.altova<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mapforce-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2010R2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The next step is to add the generated mapping-specific code to the project. We do this by copying the generated mapping specific code to our Mule project (in MapForce you can configure the package name to be used for the generated code so that makes it easier fitting in<br />
with the rest of the code). Here is a screenshot of the code to copy in this case:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/06/Screen-shot-2010-06-01-at-8.16.56-AM.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/06/Screen-shot-2010-06-01-at-8.16.56-AM-300x176.png" alt="" title="Screen shot 2010-06-01 at 8.16.56 AM" width="300" height="176" class="alignnone size-medium wp-image-1856" /></a></p>
<p>Then we created a Transformer class in the Mule project that calls the generated mapping code and returns the result.<br />
Here is an example of a Transformer which uses the generated code:</p>

<div class="wp_codebox"><table><tr id="p175314"><td class="line_numbers"><pre>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
41
42
43
44
45
46
47
48
49
</pre></td><td class="code" id="p1753code14"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">nl.redstream.transformers</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.altova.io.StreamInput</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.altova.io.StringInput</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.altova.io.StringOutput</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">nl.redstream.mapping.MappingMapTocustomer2</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mule.api.transformer.TransformerException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mule.transformer.AbstractTransformer</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 *
 * @author pascal
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomerCsvToXmlTransformer <span style="color: #000000; font-weight: bold;">extends</span> AbstractTransformer <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> CustomerCsvToXmlTransformer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        registerSourceType<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        registerSourceType<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setReturnClass<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> doTransform<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> payload, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> encoding<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> TransformerException <span style="color: #009900;">&#123;</span>
&nbsp;
        MappingMapTocustomer2 mappingToXml <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MappingMapTocustomer2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        StringOutput output <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringOutput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>payload <span style="color: #000000; font-weight: bold;">instanceof</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            StringInput input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span> payload<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                mappingToXml.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span>input, output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> TransformerException<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>payload <span style="color: #000000; font-weight: bold;">instanceof</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            StreamInput sInput <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StreamInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a><span style="color: #009900;">&#41;</span> payload<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                mappingToXml.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span>sInput, output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> TransformerException<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> output.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s it! Define the transformer as a &#8216;custom-transformer&#8217; in your Mule config and your done. You can now create and maintain all your mappings visually with MapForce.<br />
We are now looking into the capabilities that SPL (Spy Programming Language) offer, because it looks like we can generate the Mule Transformer straight from MapForce, that would even boost the producivity more!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2010/06/03/combining-mule-with-altova-mapforce/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introducing Altova Mapforce</title>
		<link>http://www.redstream.nl/2010/05/21/introducing-altova-mapforce/</link>
		<comments>http://www.redstream.nl/2010/05/21/introducing-altova-mapforce/#comments</comments>
		<pubDate>Fri, 21 May 2010 09:37:44 +0000</pubDate>
		<dc:creator>Pascal Alma</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[MapForce]]></category>
		<category><![CDATA[XML/ XSD/ XSLT]]></category>
		<category><![CDATA[Altova]]></category>

		<guid isPermaLink="false">http://blog.redstream.nl/?p=1767</guid>
		<description><![CDATA[Although we see many advantages in using (free) open source tools or frameworks we keep our eyes open to check whether the chosen open source solution is best for our customer. We recently had a situation where we preferred a &#8230; <a href="http://www.redstream.nl/2010/05/21/introducing-altova-mapforce/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.redstream.nl%2F2010%2F05%2F21%2Fintroducing-altova-mapforce%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2010%2F05%2F21%2Fintroducing-altova-mapforce%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Although we see many advantages in using (free) open source tools or frameworks we keep our eyes open to check whether the chosen open source solution is best for our customer.</p>
<p>We recently had a situation where we preferred a commercial solution over an open source one.<br />
For a specific interface, we had to map a <a href="http://en.wikipedia.org/wiki/Comma-separated_values">CSV file</a> to an <a href="http://en.wikipedia.org/wiki/XML">XML</a> file and this transformation should be processed by the Mule ESB. At first we looked at <a href="http://www.smooks.org/mediawiki/index.php?title=Main_Page">Smooks</a> to tackle this issue, as I described <a href="http://www.redstream.nl/2010/04/29/using-smooksmule-to-transform-csv-file-to-xml-one/">here</a>. It resulted in a workable solution, but as you can read in post, the transformation had to take place in two steps: first from CSV to simple XML format and secondly, a transformation from the simple XML to a more advanced XML schema. For this second transformation we made use of an <a href="http://www.w3schools.com/xsl/">XSL</a> stylesheet. We created the XSL file used for the transformation by making  use of <a href="http://www.altova.com/mapforce.html">Altova MapForce</a>.<br />
However, MapForce can do a lot more then this simple XML to XML mapping. <span id="more-1767"></span> One of the things it can do is map CSV directly to (advanced) XML. This can&#8217;t be done with just a XSL translation anymore, but MapForce can generate Java code to perform the transformation at runtime. In this post I will show how you can use MapForce to generate the code. In a next post I will show you how to combine this generated code with the <a href="http://www.mulesoft.org/">Mule ESB</a> as a <a href="http://www.mulesoft.org/documentation/display/MULE2USER/Creating+Custom+Transformers">transformer</a>.</p>
<p>The CSV file for this example looks like:</p>
<blockquote><p>
<code>firstname|lastname|city|country<br />
Steve|Jobs|Cupertino|USA<br />
Bill|Gates|Washington|USA<br />
Pascal|Alma|Papendrecht|Netherlands</code>
</p></blockquote>
<p>As you can see a pipe-separated file with labels at the first row. When we add it to MapForce as an input source we configure it like:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-csv.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-csv-300x260.png" alt="" title="mapforce-csv" width="300" height="260" class="alignnone size-medium wp-image-1784" /></a></p>
<p>The XSD to which the CSV file has to be mapped looks like:<br />
<img src="http://www.redstream.nl/wp-content/uploads/2010/05/customer-xsd.png" alt="" title="customer-xsd" width="402" height="216" class="alignnone size-full wp-image-1779" /></p>
<p>In MapForce this target is configured like this:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-xsd.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-xsd-277x300.png" alt="" title="mapforce-xsd" width="277" height="300" class="alignnone size-medium wp-image-1785" /></a></p>
<p>The mapping in MapForce is quite simple and looks like this:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-mapping.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-mapping-300x157.png" alt="" title="mapforce-mapping" width="300" height="157" class="alignnone size-medium wp-image-1786" /></a></p>
<p>The output of the mapping can be viewed in MapForce (by pressing the &#8220;Output&#8221; tab) and looks like this (for this particular input file):<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-mapping-result.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-mapping-result-242x300.png" alt="" title="mapforce-mapping-result" width="242" height="300" class="alignnone size-medium wp-image-1787" /></a></p>
<p>To perform this transformation in a Java environment we&#8217;ll generate Java code, after setting the package name that is generated by MapForce:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-mapping-setting.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-mapping-setting-300x165.png" alt="" title="mapforce-mapping-setting" width="300" height="165" class="alignnone size-medium wp-image-1788" /></a></p>
<p>The code that is generated by MapForce looks like this:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/05/generated-sources.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/05/generated-sources-179x300.png" alt="" title="generated-sources" width="179" height="300" class="alignnone size-medium wp-image-1789" /></a></p>
<p>As you can see, two packages are generated:</p>
<ul>
<li>com.altova</li>
<p>The &#8216;com.altova&#8217; package contains &#8216;generic&#8217; code that is necessary for each MapForce mapping in Java. It contains global types, functions and so on.</p>
<li>nl.redstream</li>
<p> The &#8216;nl.redstream&#8217; package is the package name we configured. This one contains the code that is specific for our mapping.
</ul>
<p>Beside these packages a Eclipse project file is generated and a Ant build script. To build this Java code you can run Ant with the following command:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/05/ant-jar.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/05/ant-jar-300x152.png" alt="" title="ant-jar" width="300" height="152" class="alignnone size-medium wp-image-1790" /></a></p>
<p>To run the code after you build it you can use the following command:<br />
<code>java nl.redstream.CustomerMappingApplication</code></p>
<p>You will get a Frame like this:<br />
<a href="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-application.png"><img src="http://www.redstream.nl/wp-content/uploads/2010/05/mapforce-application-300x254.png" alt="" title="mapforce-application" width="300" height="254" class="alignnone size-medium wp-image-1791" /></a></p>
<p>Unfortunately by using the generated Main classes, the transformation can only be done on the files that where used as input for the mapping in this code. In the next post I will show how to use this generated code and use it as a Transformer in our Mule application.</p>
<p>For a lot more info about the Java generation possiblities of MapForce look <a href="http://manual.altova.com/Mapforce/mapforce-professional/index.html?cggeneratingjavacode.htm">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2010/05/21/introducing-altova-mapforce/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

