Friday, 3 May 2013

Grouping in Biztalk Map Based on Multiple Elements.




Grouping in Biztalk Map Based on Multiple Elements.
 


Example :
<ns0:OrderRecord xmlns:ns0="http://Biztalk.Grouping.MultipleElements.Input
">
  <Data>
    <Header>
      <EMP_ID>201</EMP_ID>
      <EMP_NAME>Ram</EMP_NAME>
      <EMP_DOB>12-12-1990</EMP_DOB>
    </Header>
    <Details>
      <Product>pen</Product>
      <Quantity>2</Quantity>
      <Price>20</Price>
    </Details>
  </Data>
  <Data>
    <Header>
      <EMP_ID>202</EMP_ID>
      <EMP_NAME>Raj</EMP_NAME>
      <EMP_DOB>13-01-1989</EMP_DOB>
    </Header>
    <Details>
      <Product>Jacket</Product>
      <Quantity>3</Quantity>
      <Price>1200</Price>
    </Details>
  </Data>
  <Data>
    <Header>
      <EMP_ID>201</EMP_ID>
      <EMP_NAME>Ram</EMP_NAME>
      <EMP_DOB>12-12-1990</EMP_DOB>
    </Header>
    <Details>
      <Product>Bike</Product>
      <Quantity>1</Quantity>
      <Price>5600</Price>
    </Details>
  </Data>
 </ns0:OrderRecord>

OutPut:
<ns0:OrderRecord xmlns:ns0="http://Biztalk.Grouping.MultipleElements.Input
">
  <Data>
    <Header>
      <EMP_ID>201</EMP_ID>
      <EMP_NAME>Ram</EMP_NAME>
      <EMP_DOB>12-12-1990</EMP_DOB>
    </Header>
    <Details>
      <Product>pen</Product>
      <Quantity>2</Quantity>
      <Price>20</Price>
    </Details>
    <Details>
      <Product>Bike</Product>
      <Quantity>1</Quantity>
      <Price>5600</Price>
    </Details>
  </Data>
  <Data>
    <Header>
      <EMP_ID>202</EMP_ID>
      <EMP_NAME>Raj</EMP_NAME>
      <EMP_DOB>13-01-1989</EMP_DOB>
    </Header>
    <Details>
      <Product>Jacket</Product>
      <Quantity>3</Quantity>
      <Price>1200</Price>
    </Details>
  </Data>
</ns0:OrderRecord>

This Article group Data (Details) Records Based on Header (EMP_ID,EMP_NAME,EMP_DOB)



Just below the <xsl:output> block of our XSLT, create a key value for this grouping.  This creates an index which the transform engine can use to loop through each unique value of that key.

  <xsl:key name="groups" match="Data/Header" use="concat(EMP_ID, '|', EMP_NAME,'|',EMP_DOB)"/>

Then Loop for each key  generated by Key group

<xsl:for-each select="Data/Header[generate-id(.)=generate-id(key('groups',concat(EMP_ID, '|', EMP_NAME,'|',EMP_DOB)))]">

Then for Each Key loop all the records

<xsl:for-each select="key('groups',concat(EMP_ID, '|', EMP_NAME,'|',EMP_DOB))">
 

Custom Xslt for Grouping:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var" version="1.0" xmlns:ns0="http://Biztalk.Grouping.MultipleElements.Input
">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
    <xsl:apply-templates select="/ns0:OrderRecord" />
  </xsl:template>
  <xsl:key name="groups" match="Data/Header" use="concat(EMP_ID, '|', EMP_NAME,'|',EMP_DOB)"/>
  <xsl:template match="/ns0:OrderRecord">
    <ns0:OrderRecord>
      <xsl:for-each select="Data/Header[generate-id(.)=generate-id(key('groups',concat(EMP_ID, '|', EMP_NAME,'|',EMP_DOB)))]">
        <Data>
          <Header>
            <EMP_ID>
              <xsl:value-of select="EMP_ID/text()" />
            </EMP_ID>
            <EMP_NAME>
              <xsl:value-of select="EMP_NAME/text()" />
            </EMP_NAME>
            <EMP_DOB>
              <xsl:value-of select="EMP_DOB/text()" />
            </EMP_DOB>
          </Header>
          <xsl:for-each select="key('groups',concat(EMP_ID, '|', EMP_NAME,'|',EMP_DOB))">
            <Details>
              <Product>
                <xsl:value-of select="../Details/Product/text()" />
              </Product>
              <Quantity>
                <xsl:value-of select="../Details/Quantity/text()" />
              </Quantity>
              <Price>
                <xsl:value-of select="../Details/Price/text()" />
              </Price>
            </Details>
          </xsl:for-each>
        </Data>
      </xsl:for-each>
    </ns0:OrderRecord>
  </xsl:template>
</xsl:stylesheet>

DownLoad Code From MSDN

No comments:

Post a Comment