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()” />

<BatchNumber>

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

</BatchNumber>

</xsl:for-each>

</xsl:template>
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

About these ads

April 10, 2012 - Posted by | General | ,

4 Comments »

  1. great article.keep it up.thanks .

    Comment by neelam ojha | October 23, 2013 | Reply

  2. Great post very help full .Same thing i did in my Custom XSL.
    …..

    Comment by Satendra | April 26, 2012 | Reply

  3. [...] In Such a case you can use my other Cheat Code and Loop thorugh each repeating record using XSLT and add those items into an [...]

    Pingback by Useful Cheat Code in Mapping (Storing a Repeating Record in an Array) « BizTalk Server Tutorial | April 23, 2012 | Reply

  4. Great Post .. Will Be Very Helpful…
    Keep Posting Such Tips….

    Comment by Anonymous | April 11, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 61 other followers

%d bloggers like this: