<?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/category/technical/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>XSLT 2.0 and Java Extensions</title>
		<link>http://www.redstream.nl/2011/03/29/xslt-2-0-and-java-extensions/</link>
		<comments>http://www.redstream.nl/2011/03/29/xslt-2-0-and-java-extensions/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 19:12:55 +0000</pubDate>
		<dc:creator>Pascal Prins</dc:creator>
				<category><![CDATA[MapForce]]></category>
		<category><![CDATA[Mule]]></category>
		<category><![CDATA[XML/ XSD/ XSLT]]></category>

		<guid isPermaLink="false">http://www.redstream.nl/?p=2598</guid>
		<description><![CDATA[We use a fair lot of XSLT transformations in our projects. Since making XSLT stylesheets is a tedious and error-prone job, we use Altova MapForce to do the job. MapForce is very versatile, but can not always cope with the &#8230; <a href="http://www.redstream.nl/2011/03/29/xslt-2-0-and-java-extensions/">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%2F29%2Fxslt-2-0-and-java-extensions%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2011%2F03%2F29%2Fxslt-2-0-and-java-extensions%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>We use a fair lot of XSLT transformations in our projects. Since making XSLT stylesheets is a tedious and error-prone job, we use <a href="http://www.altova.com/mapforce.html" target="_blank">Altova MapForce</a> to do the job. MapForce is very versatile, but can not always cope with the requirements our customers come up with. This time, the customer asked us to implement some sort of maintainable translation mechanism. This mechanism should be able to translate incoming values using a user maintainable collection of values. This way, the transformation process does not need to be changed if a translation is added or updated.</p>
<p>Adding a (hardcoded) parameterized XSLT template is not an option, because this is not maintainable without changing the stylesheet. The answer? XSLT Java extensions!</p>
<p>In this blog I&#8217;ll show you a simple example on how to use Java extensions in a XSLT 2.0 stylesheet.</p>
<p><span id="more-2598"></span></p>
<p>We&#8217;ll start with a very simple &#8220;Hello World!&#8221;-isch example. I&#8217;ve created the following Java class which we will be using in our stylesheet:</p>
<h3>Our custom Java class</h3>

<div class="wp_codebox"><table><tr id="p259811"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p2598code11"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">nl.redstream.utils</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Properties</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> XsltUtils <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Your default Hello World! method.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</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> greeting<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> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello, &quot;</span> <span style="color: #339933;">+</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; !&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notice that the <code>greeting</code> method is <code>public static</code> (not really needed, but makes things easier) .</p>
<p>I packaged the project into a single jar (let&#8217;s assume it is called: <code>redstream-xslt-demo-1.0-SNAPSHOT.jar</code>) using <code>mvn clean package</code>.</p>
<h3>The XSL stylesheet</h3>
<p>Next up, creating our stylesheet ! (We will be dealing with the jar later) Use the following stylesheet:</p>

<div class="wp_codebox"><table><tr id="p259812"><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
</pre></td><td class="code" id="p2598code12"><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: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:demo</span>=<span style="color: #ff0000;">&quot;java:nl.redstream.utils.XsltUtils&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">exclude-result-prefixes</span>=<span style="color: #ff0000;">&quot;fn demo&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;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;dataset&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</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;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;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;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: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;@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:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;value&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;demo:greeting(fn:string(@value))&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</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 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;/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;/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>Notice two things:<br />
- <strong>Line 5</strong> contains a declaration of our custom namespace. Just <code>java:</code> and the FQN of your custom Java class. The prerequisite is that the class (optionally packaged in a jar) is on the classpath.<br />
- <strong>Line 15</strong> contains a reference to our Java function. Notice that you can use an Xpath expression which will be resolved at runtime and passed as an argument to the <code>greeting</code> method.</p>
<h3>Disclaimer</h3>
<p>Remember the declaration of the namespace? This is dependent of the used XSLT engine. When using Saxon, you have to use the <code>java:FQN</code> declaration. When you are using Xalan, for example, you use <code>xalan://FQN</code>. Please check the documentation of your selected XSLT engine, before you declare the namespace.</p>
<h3>XMLSpy</h3>
<p>We could perform the XSL transformation using the command line, but we are using <a target="_blank" href="http://www.altova.com/xml-editor/">Altova XMLSpy</a> for this. By default, XMLSpy uses a built-in XSLT engine, but this can be changed to a custom one. Goto: &#8220;Tools -> Options: External XSL transformation program&#8221; and copy-paste the following: <code>java -cp C:\develop\redstream-xslt-demo-1.0-SNAPSHOT.jar;C:\develop\saxon8.jar net.sf.saxon.Transform -o %2 %1 %3</code>.</p>
<p>We will be using <a target="_blank" href="http://saxon.sourceforge.net/">Saxon</a> as XSLT engine, because the endresult will be packaged in a Mule ESB application &#8230; and Mule uses Saxon (so, that&#8217;s why). Should you want to use another XSLT engine, customize the &#8216;External XSL transformation program&#8217; in XMLSpy accordingly. Notice that all necessary jar&#8217;s are copied to the <code>c:\develop</code> directory and included in the classpath (including our custom jar).</p>
<p>Assign the following XML to the XSL using <code>XSL/XQuery -> Assign sample XML file</code>:</p>

<div class="wp_codebox"><table><tr id="p259813"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p2598code13"><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;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> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;greeting&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;foo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;another&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;bar&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</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></pre></td></tr></table></div>

<p>After pressing F10 the following output should be the result of the transformation:</p>

<div class="wp_codebox"><table><tr id="p259814"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p2598code14"><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;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> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;greeting&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Hello, foo !&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;another&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Hello, bar !&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</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></pre></td></tr></table></div>

<p>What&#8217;s happened is that values <code>foo</code> and <code>bar</code> were passed to the Java <code>greeting</code> method and returned the displayed string. Of course this is a simplified example, but you&#8217;ll catch the drift. This opens up possibilites: you now can breakout of the XSLT 2.0 barriers and use Java where needed. Use your imagination to create your own custom Java class.</p>
<p>That&#8217;s it &#8230; we&#8217;ve shown that you can use custom Java classes in your XSLT 2.0 transformation. In my next post, I&#8217;ll show you how to extend Altova MapForce and make this reusable (read: drag-and-drop the Java methods graphically to auto generate the XSLT 2.0 stylesheet).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2011/03/29/xslt-2-0-and-java-extensions/feed/</wfw:commentRss>
		<slash:comments>0</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="p256416"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p2564code16"><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>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="p175319"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1753code19"><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="p175320"><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="p1753code20"><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>

