Loop for a Specific Number of Times Using Scripting Functiod in BizTalk Map

Loop for a Specific Number of Times Using Scripting Functiod in BizTalk Map



How To Loop Through Input Xml In BizTalk Map Using Scripting Functiod


Let's consider the following requirements:

For this input XML:


<ns0:TransRequest xmlns:ns0="http://microsoft.com">
  <ns0:In>
    <ns0:RecData>
      <ns0:Age>Age_0</ns0:Age>
      <ns0:FullName>A</ns0:FullName>
      <ns0:FullName>B</ns0:FullName>
      <ns0:FullName>C</ns0:FullName>
      <ns0:FullName>D</ns0:FullName>
      <ns0:FullName>E</ns0:FullName>
      <ns0:FullName>F</ns0:FullName>
    </ns0:RecData>
  </ns0:In>
</ns0:TransRequest>

we need to get this output XML:


<ns0:Request xmlns:ns0="http://microsoft.com">
  <ns0:InDocument>
    <ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>A</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>B</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>C</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>D</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>E</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>F</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>0</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>0</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>0</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>0</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>0</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>0</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>0</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>0</ns0:Name>
      </ns0:Name_Data>
      <ns0:Name_Data>
        <ns0:Name>0</ns0:Name>
      </ns0:Name_Data>
    </ns0:Name_Data>
  </ns0:InDocument>
</ns0:Request>

Where //Request/InDocument/Name_Data/Name_Data has min occurrence set to 15.  Requirement is to use BizTalk Map to perform such transformation using scripting functiod.

Sample Solution

Step 1:  Add two scripting functiods and link as shown below:

Step 2: Configure Functiod Script for # 1 and select “Inline XSLT” for “Script Type”. Write following snippet in text area for “Inline script buffer” (see image)

<xsl:call-template name="Loopable" />

 Step 3: Configure Functiod Script for # 2 and select “Inline XSLT Call Template” for “Script Type”. Write following snippet in text area for “Inline script buffer” (see image)

<xsl:template name="Loopable">
  <xsl:param name="index" select="1" />
  <xsl:param name="total" select="15" />
  <xsl:element name="ns0:Name_Data">
    <xsl:element name="ns0:Name">
      <xsl:choose>
        <xsl:when test="translate(//ns0:TransRequest/ns0:In/ns0:RecData/ns0:FullName[$index],'','') != ''">
          <xsl:value-of select="//ns0:TransRequest/ns0:In/ns0:RecData/ns0:FullName[$index]"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>0</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
  </xsl:element>
  <xsl:if test="not($index = $total)">
    <xsl:call-template name="Loopable">
      <xsl:with-param name="index" select="$index + 1" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

 Step 4: Test map and you will see the desired output.

Resource:

See Also

Another important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki
Leave a Comment
  • Please add 3 and 4 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Steef-Jan Wiggers edited Revision 10. Comment: Corrected layout

  • Sandro Pereira edited Revision 9. Comment: Add TOC

  • Sandro Pereira edited Revision 8. Comment: Add see also

  • Naomi  N edited Revision 6. Comment: Minor edit

  • SAAkhlaq edited Revision 4. Comment: Explanation added for xsl

  • SAAkhlaq edited Revision 5. Comment: Images added.

  • SAAkhlaq edited Revision 3. Comment: Images added

  • SAAkhlaq edited Revision 2. Comment: Added images

Page 1 of 1 (8 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Images are not shown

  • SAAkhlaq edited Revision 2. Comment: Added images

  • It would be helpful to include some explanation of how the XSLT works, since not everyone knows XSLT

  • SAAkhlaq edited Revision 3. Comment: Images added

  • SAAkhlaq edited Revision 4. Comment: Explanation added for xsl

  • SAAkhlaq edited Revision 5. Comment: Images added.

  • Naomi  N edited Revision 6. Comment: Minor edit

  • Sandro Pereira edited Revision 8. Comment: Add see also