Stephen W. Thomas BizTalk Blog

100% Pure BizTalk
posts - 133, comments - 195, trackbacks - 427

My Links

News

Subscribe to my blog via:


Add to Google

Visit my website at:
www.BizTalkGurus.com

Archives

BizTalk 2004 Samples

BizTalk 2006 Samples

BizTalk Videos

BizTalk White Papers

BizTalkBlogs.com

Great BizTalk Blogs

Other Links

Sorting, Grouping, and Debatching Using BizTalk Messaging

In a past edition of The BizTalker, I talked about how easy it is to use custom XSLT inside the BizTalk Mapper to sort data.  Sorting data inside the map can be doing using inline XSLT like this:

 

<xsl:for-each select="Record">

<xsl:sort select="Id" data-type="number" order="ascending"/>

<xsl:element name="Customers">

<xsl:element name="Id"><xsl:value-of select="Id" /></xsl:element>

</xsl:element>

</xsl:for-each>

 

Sometimes sorting just is not enough.  What if you also needed to group your data and break it up into single messages?  This can be done several different ways including using xpath, .net components, or something external to BizTalk all together; with none of them really being ideal. 

 

Another non-ideal way to accomplish grouping and debatching is to use pure BizTalk Messaging!  Yes, pure messaging can group and debatch your data.  Now, it is a little strange and takes a few hops through the message box, but it is a relatively simple solution to implement in a short amount of time.

 

So, how does it work?

 

Pass 1 will apply a map on the Receive Port to sort the data.  The Send Port will apply another map to group the data.

 

The grouping map using two Functoids.  One determines if the current record and the past record have the same Id.  The second does the grouping. 

 

The code in these Scripting Functoids looks like this:

 

True / False

string newNode = "";

string pastValue = "X";

 

public string MakeNewNode(string currentValue)

{

if (pastValue != currentValue)

{

   pastValue = currentValue;

   newNode = "true";

}

else

   newNode = "false";

 

return newNode;

}

 

Grouping

<xsl:template name="GroupByID">

<xsl:param name="param1"/>

<xsl:param name="param2"/>

  <xsl:if test="$param1='true'">

    <xsl:element name="Group">

      <xsl:for-each select="//Customers[Id=$param2]">

          <xsl:element name="Customer">

              <xsl:element name="Id"><xsl:value-of select="Id"/></xsl:element>

              <xsl:element name="Name"><xsl:value-of select="Name"/></xsl:element>

          </xsl:element>

      </xsl:for-each>

    </xsl:element>

  </xsl:if>

</xsl:template>

 

Pass 2 will apply an envelope to split the data inside the Receive Port and publish single messages to the message box.

 

Download the Sorting, Grouping, & Debatching Sample Code.

 

See the ReadMe.txt for set up information.  Also note that I needed two projects for this solution.  That is because the envelope schema needs to be in a separate project due to the way pipelines read the schema collections.

 

Print | posted on Monday, April 03, 2006 9:39 PM |

Feedback

Gravatar

# re: Sorting, Grouping, and Debatching Using BizTalk Messaging

Greg Forsythe pointed out an improved Grouping method using Key. Read about it in the BizTalkGurus.com forum - http://www.biztalkgurus.com/forums/viewtopic.php?t=637.
4/8/2006 1:19 PM | Stephen W. Thomas
Gravatar

# re: Sorting, Grouping, and Debatching Using BizTalk Messaging

I have an xml
<Record>
<Id="34514" Rule="46" Count="3" />
<Id="34514" Rule="46" Count="3" />
<Id="34514" Rule="46" Count="3" />
<Id="34516" Rule="47" Count="5" />
</Record>

We want to group ID and Rule based on the count field that is Id and Rule occurs 3 times and if it matches the count field then pass Id value.In this case result should be 34514.Please help me in getting this result using xslt in biztalk map please
Thanks
Saravana
3/16/2007 9:54 AM | Saravana
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: