BizTalk Training – Mapping – Muenchian Grouping and Sorting in BizTalk Maps without losing Map functionalities

There is an astonishing post by Chris Romp about Muenchian Grouping and Sorting in BizTalk Maps, but it has one limitation, by creating and configuring Custom XSL Path we lose all mapping features.

Muenchian Grouping and Sorting in BizTalk Maps

So how can we use Muenchian Grouping without losing Map features?

My First approach: I was trying to put an Inline XSLT functoid and put all the XSL inside

Muenchian Grouping and Sorting in BizTalk Maps
<xsl:key name="groups" match="Order" use="OrderId"/>
<!-- This will loop through our key ("OrderId") -->
<xsl:for-each select="Order[generate-id(.)=generate-id(key('groups',OrderId))]">
   <!-- And let's do some sorting for good measure... -->
   <xsl:sort select="OrderId" order="ascending"/>
   <Order>
      <OrderId><xsl:value-of select="OrderId/text()" /></OrderId>
      <Items>
         <!-- Another loop... -->
         <xsl:for-each select="key('groups',OrderId)">
            <ItemId><xsl:value-of select="ItemId" /></ItemId>
         </xsl:for-each>
      </Items>
   </Order>
</xsl:for-each>

The problem with that approach is that gives an error:

  • XSLT compile error at (9,8). See InnerException for details. ‘xsl:key’ cannot be a child of the ‘ns0:OutputOrder’ element

So, to avoid this error we need to separate “<xsl:key name=”groups” match=”Order” use=”OrderId”/> expression from the rest of the XSL (see the second approach)

Second Approach

Add two scripting functoids to the map>

  • In the first, configure to an “Inline XSLT Call Template” and put key expression
<xsl:key name="groups" match="Order" use="OrderId"/>
  • In the second, configure to an “Inline XSLT” and the rest of the XSL
<!-- This will loop through our key ("OrderId") -->
<xsl:for-each select="Order[generate-id(.)=generate-id(key('groups',OrderId))]">
   <!-- And let's do some sorting for good measure... -->
   <xsl:sort select="OrderId" order="ascending"/>
   <Order>
      <OrderId><xsl:value-of select="OrderId/text()" /></OrderId>
      <Items>
         <!-- Another loop... -->
         <xsl:for-each select="key('groups',OrderId)">
            <ItemId><xsl:value-of select="ItemId" /></ItemId>
         </xsl:for-each>
      </Items>
   </Order>
</xsl:for-each>
Muenchian Grouping and Sorting in BizTalk Maps

See Sample 1, map “MapOrder.btm”

How can we improved (a little more) this solution

When leading with large files, speed processing is vital. Classical Muenchian grouping use generate-id(). Muenchian grouping using generate-id() is the slowest than using count() function, and shows the worst scalability. Probably the reason is poor generate-id() function implementation. In other words, count() function performs is much better.

So to improve Meunchian a little more we have to use the count() function instead of generate-id():

<xsl:for-each select="Order[count(. | key('groups',OrderId)[1]) = 1]">
  • See Sample 1, map “MapOrder2.btm”

Here are some performance stats that I found (see original post):

performance stats Muenchian Grouping

The graph view works better:

Muenchian Grouping and Sorting in BizTalk Maps

Download

THIS POWERSHELL IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND.

You can download Muenchian Grouping and Sorting in BizTalk Maps without losing Map functionalities from GitHub here:

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

7 thoughts on “BizTalk Training – Mapping – Muenchian Grouping and Sorting in BizTalk Maps without losing Map functionalities”

  1. Both articles are lovely and usefull. I faced one issue when I configured what Chris suggested. Inside VS, testing map gives expected outcome. When I deployed the application, created a recieve location and port (configured map on receive port), also configured a send port which subscribes to the rport, droping the sampl file, I get an ouput file exactly the same as the input which is Order222|Item123Order111|Item456Order222|Item789as if map is not appliedAm I missing something, thankls for your help

  2. Now I tried your maps inside my application configured Receive Port and Send port, outcome is the same, no xml is coming out, the same flat file is send to ourput directory. Did you apply your solution in a run time environment?Thanks for your help

  3. Hi,Yes I apply the solution in run time environment.Both solutions, Chris Romp and mine, work well.Just deploy the schemas and map into a BizTalk application, and then: • Create new ReceivePort (one-way) ex: ReceivePortMG o New Receice Location: Type: FILE, Receive pipeline: XMLReceive o Inbound Map: config the map, In my case o Source: InputOrder o Map: MapOrder o Target: OutputOrder • Create SendPort (static one-way): Type: FILE, Send pipeline: XMLTransmit o Apply a filter to the send port o Property: BTS.ReceivePortName o Operator: == o Value: ReceivePortMGStart the ports.

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top