BizTalk Server Tutorial

BizTalk Server Concepts and Common Errors

Useful Cheat Code in Mapping (Calling C# code from XSLT)

I am going to show you a very useful cheat code while doing complex mapping.

When you do complex mapping especially with EDI / HL7 Schemas, we may not be able to achieve the desired mapping using out of box Functoids. In such cases, we go for using Inline XSLT / Inline XSLT Call Template.

While using this inline XSLT, there are some tasks which are complex to develop using XSLT.

For example, what if I want to increment a variable by 2, for each input Record and map the resulting value to a Target element? In XSLT we don’t have an option of using x=x+2.

In these cases we can combine C# and XSLT and achieve the desired output.

1. Create a Global Variable in a Scripting Functoid and write a method to increment the variable by 2, for each method call.

2. Leave the scripting Functoid as it is. It is not required to connect this to any input/output node.
3. Now take another scripting Functoid and select Inline XSLT Call Template and add your XSLT.
4. In order to call the above C# method from XSLT and get the incremented value, use the below line of code.

<xsl:variable name=”var:counter” select=”userCSharp:IncrementAndReturn()” />

5. Complete XSLT will look like below.

<xsl:template name=”MyXsltConcatTemplate”>

<xsl:for-each select=”Employees/Employee”>

<xsl:variable name=”var:counter” select=”userCSharp:IncrementAndReturn()” />


<xsl:value-of select=”$var:counter” />



6. Completed map will be like below.

This way we can leverage C# features and combine that with the flexibility of XSLT and get the desired output.

Note: This is only a sample to show how to call C# methods from XSLT. However, the above sample can be created with basic Functoids and doesn’t require XSLT. This is to give you all an idea of using XSLT and C# together.

Hope this helps

– Shiv

April 10, 2012 Posted by | General | , | 5 Comments