<?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>Gobbledygooks by Richard Hallgren &#187; XSLT</title>
	<atom:link href="http://www.richardhallgren.com/category/xslt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardhallgren.com</link>
	<description>.NET, BizTalk and integration focused scribbles</description>
	<lastBuildDate>Fri, 23 Apr 2010 07:13:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Use code blocks to extend your BizTalk custom XSLT maps</title>
		<link>http://www.richardhallgren.com/use-code-blocks-to-extend-your-biztalk-custom-xslt-maps/</link>
		<comments>http://www.richardhallgren.com/use-code-blocks-to-extend-your-biztalk-custom-xslt-maps/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 07:38:47 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[BizTalk 2006]]></category>
		<category><![CDATA[BizTalk 2009]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://www.richardhallgren.com/use-code-blocks-to-extend-your-biztalk-custom-xslt-maps/</guid>
		<description><![CDATA[Update 2010-04-13       Grant Samuels commented and made me aware of the fact that inline scripts might in some cases cause memory leaks. He has some further information here and you’ll find a kb-article here.&#160;

I’ve posted a few times before on how powerful I think it is in complex mapping [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Update 2010-04-13       <br /></strong>Grant Samuels commented and made me aware of the fact that inline scripts might in some cases cause memory leaks. He has some further information <a href="http://linderalex.blogspot.com/2008/06/memory-leak-using-biztalk-mapper.html" target="_blank">here</a> and you’ll find a kb-article <a href="http://support.microsoft.com/kb/918643" target="_blank">here</a>.<strong>&#160;</strong></p>
</blockquote>
<p>I’ve posted a few times before on how powerful I think it is in complex mapping to be able to replace the BizTalk Mapper with a custom XSLT script (<a href="http://msdn.microsoft.com/en-us/library/aa560154(BTS.20).aspx">here’s how to</a>). The BizTalk Mapper is nice and productive in simpler scenarios but in my experience it break down in more complex ones and maintaining a good overview is <em>hard</em>. I’m however looking forward to the new version of the <a href="http://www.microsoft.com/biztalk/en/us/roadmap.aspx#2009r2">tool in BizTalk 2010</a> – but until then I’m using custom XSLT when things gets complicated.</p>
<p>Custom XSLT however lacks a few things once has gotten used to have &#8211; such as scripting blocks, clever functoids etc. In some previously post (<a href="http://www.richardhallgren.com/how-the-extend-a-custom-xslt-in-biztalk-using-exslt-and-the-mvpxml-project/">here</a> and <a href="http://www.richardhallgren.com/using-xslt-1-0-to-summarize-a-node-set-with-comma-separated-values/">here</a>) I’ve talked about using EXSLT as a way to extend the capabilities of custom XSLT when used in BizTalk. </p>
<h2>Bye, bye external libraries – heeeello inline scripts <img src='http://www.richardhallgren.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </h2>
<p>Another way to achieve much of the same functionality even easier is to use embedded scripting that’s supported by the XslTransform class. Using a script block in XSLT is easy and is also the way the BizTalk Mapper makes it possible to include C# snippets right into your maps.</p>
<p>Have a look at the following XSLT sample:</p>
<pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;xsl:stylesheet
    version=&quot;1.0&quot;
    xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
    xmlns:msxsl=&quot;urn:schemas-microsoft-com:xslt&quot;
    xmlns:code=&quot;http://richardhallgren.com/Sample/XsltCode&quot;
    exclude-result-prefixes=&quot;msxsl code&quot;
    &gt;
    &lt;xsl:output method=&quot;xml&quot; indent=&quot;yes&quot;/&gt;

    &lt;xsl:template match=&quot;@* | node()&quot;&gt;
        &lt;Test&gt;
            &lt;UniqueNumber&gt;
                &lt;xsl:value-of select=&quot;code:GetUniqueId()&quot; /&gt;
            &lt;/UniqueNumber&gt;
            &lt;SpecialDateFormat&gt;
                &lt;xsl:value-of select=&quot;code:GetInternationalDateFormat('11/16/2003')&quot; /&gt;
            &lt;/SpecialDateFormat&gt;
            &lt;IncludesBizTalk&gt;
                &lt;xsl:value-of select=&quot;code:IncludesSpecialWord('This is a text with BizTalk in it', 'BizTalk')&quot; /&gt;
            &lt;/IncludesBizTalk&gt;
        &lt;/Test&gt;
    &lt;/xsl:template&gt;

    &lt;msxsl:script language=&quot;CSharp&quot; implements-prefix=&quot;code&quot;&gt;
        //Gets a unique id based on a guid
        public string GetUniqueId()
        {
            return Guid.NewGuid().ToString();
        }

        //Formats US based dates to standard international
        public string GetInternationalDateFormat(String date)
        {
            return DateTime.Parse(date, new System.Globalization.CultureInfo(&quot;en-US&quot;)).ToString(&quot;yyyy-MM-dd&quot;);
        }

        //Use regular expression to look for a pattern in a string
        public bool IncludesSpecialWord(String s, String pattern)
        {
            Regex rx = new Regex(pattern);
            return rx.Match(s).Success;
        }
    &lt;/msxsl:script&gt;
&lt;/xsl:stylesheet&gt;</pre>
<p>All one has to do is to define a code block, reference the xml-namespace used and start coding! Say goodbye to all those external library dlls!</p>
<p>It’s possible to use a few core namespaces without the full .NET namespace path but all namespaces are available as long as they are fully qualified. MSDN has a great page with all the details <a href="http://msdn.microsoft.com/en-us/library/533texsx(VS.71).aspx">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardhallgren.com/use-code-blocks-to-extend-your-biztalk-custom-xslt-maps/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using XSLT 1.0 to summarize a node-set with comma separated values</title>
		<link>http://www.richardhallgren.com/using-xslt-1-0-to-summarize-a-node-set-with-comma-separated-values/</link>
		<comments>http://www.richardhallgren.com/using-xslt-1-0-to-summarize-a-node-set-with-comma-separated-values/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 09:31:50 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://www.richardhallgren.com/using-xslt-1-0-to-summarize-a-node-set-with-comma-separated-values-2/</guid>
		<description><![CDATA[Pure XSLT is very powerful but it definitely has its weaknesses (I’ve written about how to extend XSLT using mapping and BizTalk previously here) … One of those are handling numbers that uses a different decimal-separator than a point (“.”).
Take for example the XML below
&#60;Prices&#62;
  &#60;Price&#62;10,1&#60;/Price&#62;
  &#60;Price&#62;10,2&#60;/Price&#62;
  &#60;Price&#62;10,3&#60;/Price&#62;
&#60;/Prices&#62;
Just using the XSLT sum-function [...]]]></description>
			<content:encoded><![CDATA[<p>Pure XSLT is very powerful but it definitely has its weaknesses (I’ve written about how to extend XSLT using mapping and BizTalk previously <a href="http://www.richardhallgren.com/how-the-extend-a-custom-xslt-in-biztalk-using-exslt-and-the-mvpxml-project/" target="_blank">here</a>) … One of those are handling numbers that uses a different decimal-separator than a point (“.”).</p>
<p>Take for example the XML below</p>
<pre class="brush: xml;">&lt;Prices&gt;
  &lt;Price&gt;10,1&lt;/Price&gt;
  &lt;Price&gt;10,2&lt;/Price&gt;
  &lt;Price&gt;10,3&lt;/Price&gt;
&lt;/Prices&gt;</pre>
<p>Just using the <a href="http://www.zvon.org/xxl/XSLTreference/Output/function_sum.html" target="_blank">XSLT sum-function</a> on these values will give us a “NaN” values. To solve it we’ll have to use recursion and something like in the sample below.</p>
<p>The sample will select the node-set to summarize and send it to the “SummarizePrice” template. It will then add the value for the first Price tag of the by transforming the comma to a point. It will then check if it’s the last value and if not use recursion to call into itself again with the next value. It will  keep adding to the total amount until it reaches the last value of the node set.</p>
<pre class="brush: xml;">&lt;xsl:template match="Prices"&gt;
  &lt;xsl:call-template name="SummurizePrice"&gt;
    &lt;xsl:with-param name="nodes" select="Price" /&gt;
  &lt;/xsl:call-template&gt;
&lt;/xsl:template&gt;

&lt;xsl:template name="SummurizePrice"&gt;
  &lt;xsl:param name="index" select="1" /&gt;
  &lt;xsl:param name="nodes" /&gt;
  &lt;xsl:param name="totalPrice" select="0" /&gt;

  &lt;xsl:variable name="currentPrice" select="translate($nodes[$index], ',', '.')"/&gt;

  &lt;xsl:choose&gt;
    &lt;xsl:when test="$index=count($nodes)"&gt;
      &lt;xsl:value-of select="$totalPrice + $currentPrice"/&gt;
    &lt;/xsl:when&gt;
    &lt;xsl:otherwise&gt;
      &lt;xsl:call-template name="SummurizePrice"&gt;
        &lt;xsl:with-param name="index" select="$index + 1" /&gt;
        &lt;xsl:with-param name="totalPrice" select="$totalPrice + $currentPrice" /&gt;
        &lt;xsl:with-param name="nodes" select="$nodes" /&gt;
      &lt;/xsl:call-template&gt;
    &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;

&lt;/xsl:template&gt;</pre>
<p>Simple but a bit messy and nice to have for future cut and paste <img src='http://www.richardhallgren.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardhallgren.com/using-xslt-1-0-to-summarize-a-node-set-with-comma-separated-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How the extend a custom Xslt in BizTalk using EXSLT and the Mvp.Xml project</title>
		<link>http://www.richardhallgren.com/how-the-extend-a-custom-xslt-in-biztalk-using-exslt-and-the-mvpxml-project/</link>
		<comments>http://www.richardhallgren.com/how-the-extend-a-custom-xslt-in-biztalk-using-exslt-and-the-mvpxml-project/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 20:15:55 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[BizTalk 2006]]></category>
		<category><![CDATA[EXSLT]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://www.richardhallgren.com/how-the-extend-a-custom-xslt-in-biztalk-using-exslt-and-the-mvpxml-project/</guid>
		<description><![CDATA[Lately I&#8217;ve been using custom Xslt more and more instead of the BizTalk mapping tool. I still use the mapping tool in easy scenarios when I just need to do some straight mapping or maybe even when I need to concatenate some fields, but as soon as I need to to some looping, grouping, calculations [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been using custom Xslt <a href="http://www.richardhallgren.com/efficient-grouping-and-debatching-of-big-files-using-biztalk-2006/" target="_blank">more</a> and more instead of the BizTalk mapping tool. I still use the mapping tool in easy scenarios when I just need to do some straight mapping or maybe even when I need to concatenate some fields, but as soon as I need to to some looping, grouping, calculations etc I&#8217;ve made a <em>promise</em> to myself to use custom Xslt!</p>
<p>I find custom Xslt so much easier in more complex scenarios and once one get past the template matching and understands how and when to use recursion (No you can&#8217;t reassign a variable in Xslt and you&#8217;re not supposed to!) I find it to be a <strong>dream</strong> compared to the mapping tool. I also find the code so much easier to maintain compared to the result from the mapping tool. I mean someone would have to pay me good money to even start figuring out what <a href="http://www.edsquared.com/content/binary/WindowsLiveWriter/DebugthatBizTalkMap_E215/BadMap_thumb.png" target="_blank">this</a> map is doing. And the scary thing is that if you worked with BizTalk for a while you probably know that maps like this isn&#8217;t that rare! I&#8217;ve even seen worse!</p>
<p>Don&#8217;t get me wrong, Xslt definitely has some <strong>major</strong> <a href="http://biglist.com/lists/xsl-list/archives/200102/msg01384.html" target="_blank">limitations</a>.</p>
<blockquote><p>Some of the acute limitations of XSLT 1.0 I can think of off the top of my head are: </p>
<ul>
<li>The lack of real string comparison
<li>No support for dates
<li>No simple mechanism for grouping
<li>No querying into RTF&#8217;s </li>
</ul>
</blockquote>
<p>And it doesn&#8217;t take long before one runs up against one of these and suddenly you wish you were <strong>back</strong> in mapping tool were we just could add scripting functoid and some code or a external assembly. But then you remember &#8230; (Sorry, I know it&#8217;s painful just to watch it).</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/09/windowslivewriterhowtheextendacustomxsltinbizt.xmlproject-138fabadmap-thumb-2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="400" alt="BadMap_thumb" src="http://www.richardhallgren.com/wp-content/uploads/2008/09/windowslivewriterhowtheextendacustomxsltinbizt.xmlproject-138fabadmap-thumb-thumb.png" width="504" border="0"></a> </p>
<p><strong>There has to be a better way of doing this and <em>combining</em> the best out of the two worlds! </strong></p>
<p>I started looking into to how BizTalk actually solves combining Xslt and the possibility to use external assemblies. After a couple of searches I found Yossi&#8217;s <a href="http://www.sabratech.co.uk/blogs/yossidahan/archive/2005_08_01_archive.html" target="_blank">nice article</a> that explained it to me (from 2005! I&#8217;m behind on this one!) and it even turns out that there an <a href="http://msdn.microsoft.com/en-us/library/ms966408.aspx">example</a> in the BizTalk SDK. </p>
<p>Ok, so now I had what I need. I started a new class library project and began writing some date parsing methods, some padding methods and so on. </p>
<p>It somehow however felt wrong from the start and I got this grinding feeling that I must be reinventing the wheel (I mean these are well know limitations of Xslt and must have been solved before). Even worse I also felt that I was creating a stupid <em>single point of failure</em> as I started using the component from all different maps in my projects and I have actually seen how much pain a bug in similar shared dll:s could cause. Basically <em>a small bug in the component could halt all the process using the library</em>! Finally I realized that this kind of library would be under constant development as we ran into more and more areas of limitations in the our Xslt:s and that would just <strong>increase</strong> the risk of errors and mistakes. </p>
<p>After some further investigation I found <a href="http://www.exslt.org/">EXSLT</a> which looked like a solution to my problems! A <em>stable, tested</em> library of Xslt extensions that we could take dependency on as <strong>it&#8217;s unlikely to have any bugs and that should include the functionality we&#8217;re missing in standard Xslt!</strong></p>
<h3>How I used EXSLT in BizTalk</h3>
<p>These days it&#8217;s the <a href="http://www.codeplex.com/MVPXML/People/ProjectPeople.aspx">Xml Mvp crowd</a> over at the <a href="http://www.codeplex.com/MVPXML">Mvp.Xml project</a> who develops and maintains the .NET implementation of EXSLT. So I downloaded the latest <a href="http://www.codeplex.com/MVPXML/Release/ProjectReleases.aspx?ReleaseId=4894">binaries</a> (version 2.3). Put the the <em>Mvp.Xml.dll</em> in the GAC. Wrote a short custom extension Xml snippet that looked like this (using what I&#8217;ve learnt from <a href="http://www.sabratech.co.uk/blogs/yossidahan/archive/2005_08_01_archive.html">Yossi&#8217;s article</a>).</p>
<pre class="xml" name="code">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;ExtensionObjects&gt;
    &lt;ExtensionObject
     Namespace="http://exslt.org/dates-and-times"
     AssemblyName="Mvp.Xml,
     Version=2.3.0.0, Culture=neutral,
     PublicKeyToken=6ead800d778c9b9f"
     ClassName="Mvp.Xml.Exslt.ExsltDatesAndTimes"/&gt;
&lt;/ExtensionObjects&gt;</pre>
<p>All you define is the <em>Xml namespace </em>you like to use in your Xslt to reference the dll, the <em>full assembly name</em> and finally the <em>name of the class</em> in <em>Mvp.Xml.Exslt</em> you want to use (make sure you also download the source to Xml.Mvp, it helps when looking up in what classes and namespaces different methods are placed). </p>
<p>That means you need one <em>ExtensionObjects</em> block for each class you want you use which really isn&#8217;t a problem as the methods are nicely structured based on there functionality.</p>
<p>Then we can use this in a Xslt like this:</p>
<pre class="xml" name="code">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:S1="http://ExtendedMapping.Schema1"
                xmlns:S2="http://ExtendedMapping.Schema2"
                xmlns:exslt="http://exslt.org/dates-and-times"
                version="1.0"&gt; 

    &lt;xsl:template match="/"&gt;
        &lt;S2:Root&gt;
            &lt;Field&gt;
                &lt;xsl:value-of select="exslt:dateTime()"/&gt;
            &lt;/Field&gt;
        &lt;/S2:Root&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p>Which gives us the below output. <strong>Notice the current time and date! Cool!</strong></p>
<pre class="xml" name="code">&lt;S2:Root xmlns:S1="http://ExtendedMapping.Schema1" xmlns:S2="http://ExtendedMapping.Schema2" xmlns:exslt="http://exslt.org/dates-and-times"&gt;
  &lt;Field&gt;2008-09-05T20:45:13+02:00&lt;/Field&gt;
&lt;/S2:Root&gt;</pre>
<p>All you then have to do in you map is to reference the Xslt and the extension Xml.</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/09/windowslivewriterhowtheextendacustomxsltinbizt.xmlproject-138facustom-extension-in-map-2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="269" alt="Custom Extension In Map" src="http://www.richardhallgren.com/wp-content/uploads/2008/09/windowslivewriterhowtheextendacustomxsltinbizt.xmlproject-138facustom-extension-in-map-thumb.png" width="431" border="0"></a> </p>
<p>Just as final teaser I&#8217;ll paste a few methods from the <a href="http://www.exslt.org/">EXSLT documentation</a></p>
<p>Some <em>string</em> methods:</p>
<ul>
<li>str:align()
<li>str:concat()
<li>str:decode-uri()
<li>str:encode-uri()
<li>str:padding()
<li>str:replace()
<li>str:split()
<li>str:tokenize() </li>
</ul>
<p>Some <em>date and time</em> methods:</p>
<ul>
<li>date:add()
<li>date:add-duration()
<li>date:date()
<li>date:date-time()
<li>date:day-abbreviation()
<li>date:day-in-month()
<li>date:day-in-week()
<li>date:day-in-year()
<li>date:day-name()
<li>date:day-of-week-in-month()
<li>date:difference()
<li>date:duration()
<li>date:format-date()
<li>date:hour-in-day()
<li>date:leap-year()
<li>date:minute-in-hour()
<li>date:month-abbreviation()
<li>date:month-in-year()
<li>date:month-name()
<li>date:parse-date()
<li>date:second-in-minute()
<li>date:seconds()
<li>date:sum()
<li>date:time()
<li>date:week-in-month()
<li>date:week-in-year()
<li>date:year() </li>
</ul>
<p>As if this was enough (!) the Mvp Xml project added a couple of there own methods! What about <strong><em>string lowercase</em></strong> and <strong><em>string uppercase &#8211; </em>all in Xslt!</strong> And about 30 new date-time related methods extra to the standard ones already in EXSLT!</p>
<p>Check out the full documentation <a href="http://www.xmllab.net/mvpxml/default.aspx" target="_blank">here</a>!</p>
<p>Let me know how it works out for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardhallgren.com/how-the-extend-a-custom-xslt-in-biztalk-using-exslt-and-the-mvpxml-project/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
