BizTalk Training – Mapping – How to implement multi-level Muenchian grouping in BizTalk Maps

Posted: May 15, 2012  |  Categories: BizTalk Maps

I already talked about Muenchian Grouping in BizTalk Maps in the past:

Inspired by a question in BizTalk Server Forums: XSLT Mapping Question (Summing Values)… I decided to solve this problem and publish this sample to help this user… what can I say… I simply love mapping problems!!!

So how can we perform multi-level Muenchian grouping in BizTalk Maps and perform some mathematical operations on this group?

You can read more about the problem in the thread, nut basically the problem is: To sum all records and create a unique record for records having the same account type and city!

Solution

multi-level Muenchian grouping

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="Record" use="concat(City, '|', AccountType)"/>
  • In the second, configure to an “Inline XSLT” and the rest of the XSL
    <xsl:for-each select="Record[generate-id(.)=generate-id(key('groups',concat(City, '|', AccountType)))]">
       <Record>
          <xsl:variable name="city" select="City/text()" />
          <xsl:variable name="type" select="AccountType/text()" />
    
          <AccountType>
             <xsl:value-of select="$type" />
          </AccountType>
          <City>
             <xsl:value-of select="$city" />
          </City>
    
          <xsl:variable name="negativeTotal" select="sum(//Record[(City = $city) and (AccountType = $type) and (Sign = '-')]/Price)" />
          <xsl:variable name="positiveTotal" select="sum(//Record[(City = $city) and (AccountType = $type) and (Sign = '+')]/Price)" />
          <xsl:choose>
             <xsl:when test="$positiveTotal &gt; $negativeTotal">
                <Sign>+</Sign>
                <Price>
                   <xsl:value-of select="$positiveTotal - $negativeTotal" />
                </Price>
             </xsl:when>
             <xsl:otherwise>
                <Sign>-</Sign>
                <Price>
                   <xsl:value-of select="$negativeTotal - $positiveTotal" />
                </Price>
             </xsl:otherwise>
          </xsl:choose>
       </Record>
    </xsl:for-each>
  • Drag a link from the Second Scripting Functoid to the record “Record” in the destination schema;

You can download the source code from:
How to implement multi-level Muenchian grouping in BizTalk MapsHow to implement multi-level Muenchian grouping in BizTalk Maps
GitHub

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.

5 thoughts on “BizTalk Training – Mapping – How to implement multi-level Muenchian grouping in BizTalk Maps”

    1. Hi Erik,

      There are several ways to solve a particular mapping problem; in this particular case I could use a pure XSLT map.

      So why would I use the mapper at all? I think that in the long term, this way is more readable than using an pure XSLT map

      And if it had different records I wouldn’t lose the BizTalk Mapper functionalities.

      I normally like to use the best of both worlds: the best of custom XSLT and the best of BizTalk Mapper functionalities. However in some case doing a pure XSLT map makes the map more efficient (performance)

  1. Hi Sandro,

    If all elements in your schema have a namespace prefix on it such as “ns0:” , I am unable to get your sample to work. What do you need to do for the “key” and the for-each statement to key this to work.
    In many cases, the schemas we have to work contain the namespace prefix for each element.

  2. Hi Sandro,

    How does your example work when every element in the sample schema contains the namespace prefix of ns0:

    What do we have to change in the Key and for-each statement in order for this to work ?

Leave a Reply

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

turbo360

Back to Top