Share via


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 an extensive amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki