Geeks With Blogs
Arun Manick - BizTalk My work in BizTalk
| Home |

 

Problem: One of the most common scenarios in BizTalk is calculating the record count of repeating structure. BizTalk has come up with an advanced functoid called Record Count functoid which will give the record count for the repeating structure however you cannot define the scope for a Record Count functoid. Because Record Count functoid accepts exactly one parameter which can be repeating record or field element.

 

If somebody don’t know what “scope” means I will explain with a simple example. Consider that we have a source schema having a structure Country -> State -> City. Country will have various states and each state will have different cities. Now you want to calculate no. of cities present in each state. Here scope is defined at the parent node “State”. Traditional Record Count functoid will give the total no. of cities present in the source message and not the State level city count.

 

Source Schema:

 

Destination Schema:

 

Soultion #1: As the title indicates we are not going to add one more parameter to the record count functoid. Instead of that, we are going to achieve the solution with the help of Scripting functoid with Inline XSLT script. XSLT is basically the transformation language used in the mapping.

 

 

“No.OfCities” indicates the destination field name to which we are going to send the value. In count(City), “count” refers to built in XPath function used in XSLT and “City” refers to source schema record name. Here you can find the list of built-in functions available in XSLT.

 

The mapping will look like as follows:

 

The 2 Record Count functoids used in this map will give the total number of states and total number of cities as that of input message.

 

Soultion #2:  If someone doesn’t like XSLT code and they wish to achieve the solution using functoids alone, then here is another solution.

 

  1. Use logical Existence functoid to check whether “City” exist or not
  2. Connect the output of Logical Existence functoid to the Value Mapping functoid with second parameter as constant “1”. Hence if the first parameter is TRUE it will give the output as “1”.
  3. Connect the output of Value Mapping functoid to the Cumulative Sum functoid with scope as “1”

 

This will calculate the City count at the state level. The mapping will look like as follows:

 

 

Let us see the sample input and the map output.

 

Input:

<?xml version="1.0" encoding="utf-8"?>

<ns0:Country xmlns:ns0="http://RecordCount.Source">

  <State>

    <StateName>Tamilnadu</StateName>

    <City>

      <CityName>Pollachi</CityName>

    </City>

    <City>

      <CityName>Coimbatore</CityName>

    </City>

    <City>

      <CityName>Chennai</CityName>

    </City>

  </State>

  <State>

    <StateName>Kerala</StateName>

    <City>

      <CityName>Palakad</CityName>

    </City>

  </State>

  <State>

    <StateName>Karnataka</StateName>

    <City>

      <CityName>Bangalore</CityName>

    </City>

    <City>

      <CityName>Mangalore</CityName>

    </City>

  </State>

</ns0:Country>

 

  Output:

<ns0:Country xmlns:ns0="http://RecordCount.Destination">

          <No.OfStates>3</No.OfStates>

          <No.OfCities>6</No.OfCities>

          <States>

                   <No.OfCities>3</No.OfCities>

          </States>

          <States>

                   <No.OfCities>1</No.OfCities>

          </States>

          <States>

                   <No.OfCities>2</No.OfCities>

          </States>

</ns0:Country>

 

Conclusion:
Both the solution will give the same output however we should look for the performance as well. Inline XSLT will be good in terms of performance because we are directly writing the XSLT code. If you use functoids BizTalk mapper will internally generate the XSLT code for our business logic. For both these maps if you validate and see the XSL code means you can understand which one is optimum solution. Solution#2 will generate massive C# code and imagine you want to perform this operation with input message having 100000 States. Definitely Solution#1 will give the better result.
 
Source Code:
I have shared the complete source code and you can download it from here.
 

Hope you enjoyed my post. 

-Arun

 
Posted on Wednesday, April 14, 2010 6:10 PM BizTalk Mapping | Back to top


Comments on this post: Defining scope for Record Count functoid:

# re: Defining scope for Record Count functoid:
Requesting Gravatar...
Good one Arun.. thanks for sharing.. Keep posting
Left by Bijesh on Apr 14, 2010 9:57 PM

# re: Defining scope for Record Count functoid:
Requesting Gravatar...
Clear Explanation and problem/solution approach. Good Article Arun.
It's interesting and Keep Writing.

Besh Wishes,
Venkatesh R
Left by Venkatesh R on Apr 15, 2010 10:12 AM

# re: Defining scope for Record Count functoid:
Requesting Gravatar...
Thanks Arun..Good Reading... Thats a lot simple and good explanation.
Left by Ranjith Rajan U on Apr 15, 2010 10:27 AM

# re: Defining scope for Record Count functoid:
Requesting Gravatar...
Solution #2 is interesting... Thanks for sharing...
Left by Sathish on Oct 13, 2011 10:09 AM

# re: Defining scope for Record Count functoid:
Requesting Gravatar...
That is a lot of explanation there. Thanks for sharing Arun. More power to you. I like the second solution better. Thanks again.

Krista N. Esparza
Left by proactive reviews on Feb 06, 2012 2:04 PM

# re: Defining scope for Record Count functoid:
Requesting Gravatar...
good article , the way you have explained is excellent...
Left by lalitha on Jan 05, 2016 4:59 PM

Your comment:
 (will show your gravatar)


Copyright © ArunManick | Powered by: GeeksWithBlogs.net