BizTalk Server 2004 : B2B, EAI and .NET

Saturday, September 18, 2004

Using recursion in the In-line XSLT Template type Scripting Functoid

People using In-Line XSLT Templates in their scripting functoids often run into the need for looping, WITHOUT using an <xsl:for-each>. An example in the BizTalk context would be, if you had a comma separated string in the source schema and wanted to tokenize it into multiple destination schema elements. The following XSLT template does that for you.

<xsl:template name="TokenizeCommaSepString">
<xsl:param name="stringToTokenize" />
<xsl:param name="destinationElementName" />

<xsl:if test="$stringToTokenize != ''">
<xsl:choose>
<xsl:when test="contains($stringToTokenize, ',')=0">
<xsl:element name="{$destinationElementName}">
<xsl:value-of select="$stringToTokenize" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$destinationElementName}">
<xsl:value-of select="substring-before($stringToTokenize, ',')" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>

<xsl:call-template name="TokenizeCommaSepString">
<xsl:with-param name="stringToTokenize" select="substring-after($stringToTokenize,',')" />
<xsl:with-param name="destinationElementName" select="$destinationElementName" />
</xsl:call-template>

</xsl:if>
</xsl:template>

Drag a Scripting functoid onto the BizTalk Mapper. Go to properties and choose Configure functoid script. Choose Inline XSLT Call Template as the Script Type. Paste the above code in the Script Buffer text area. This functoid will take the comma separated string in as the first parameter and the destination element name as the second parameter. It will split the string into parts and create a sequence of elements in the destination message.

Observe the use of recursion (marked in bold), to solve similar problems. Have any questions, post a comment.


6 Comments:

  • This comment has been removed by a blog administrator.

    By Blogger Saravana, on October 09, 2004 7:30 PM  

  • Ram, i can help, if u leave ur contact details.

    By Blogger Devdutt, on October 11, 2004 7:02 PM  

  • Dev
    Thanks for your reply.Can you mail me your contact details at gnsrk@yahoo.com
    Thanks
    Ramkumar

    By Blogger Saravana, on October 12, 2004 3:40 PM  

  • Hello Dev,

    I Just try to solve a problem in BizTalk2004 with the Mapper and I found your sample for recursion in XSLT, which unfortunately does not suite to my situation.

    what I have:

    Incoming:

    GitemH
    Gentity_propertyH
    Gcontrol_idHBegin_TimeG/control_idH
    Gprop_valuuH170000$090000$083000G/prop_valueH
    G/entity_propertyH
    Gentity_propertyH
    Gcontrol_idHEnd_TimeG/control_idH
    Gprop_valuuH190000$100000$123000G/prop_valueH
    G/entity_propertyH

    G/itemH

    Outgoing:

    GUserH
    GBegin_TimeH170000G/Begin_TimeH
    GEnd_TimeH190000G/End_TimeH
    G/UserH
    GUserH
    GBegin_TimeH090000G/Begin_TimeH
    GEnd_TimeH100000G/End_TimeH
    G/UserH
    GUserH
    GBegin_TimeH083000G/Begin_TimeH
    GEnd_TimeH123000G/End_TimeH
    G/UserH


    Any ideas how to solve this? Next to the recursion there must be a group, which I just don't know how to manage at this time... :-(

    Best eregards
    Patrick

    By Anonymous Patrick, on February 17, 2005 8:37 AM  

  • Patrick,
    sorry for the delay, i've been outta town. Will look into the problem and get back to you shortly. you can send replies to pyrogenic@hotmail.com.
    Regards
    Dev

    By Blogger Devdutt, on February 22, 2005 7:43 AM  

  • You must use a variable in place of the second param

    <xsl:template name="TokenizeCommaSepString">
    <xsl:param name="SomeStringConcat" />
    <xsl:variable name="destElem" select="'destElem'" />

    <xsl:if test="$SomeStringConcat != ''">
    <xsl:choose>
    <xsl:when test="contains($SomeStringConcat, ';')=0">
    <xsl:element name="{$destElem}">
    <xsl:value-of select="$SomeStringConcat" />
    </xsl:element>
    </xsl:when>
    <xsl:otherwise>
    <xsl:element name="{$destElem}">
    <xsl:value-of select="substring-before($SomeStringConcat, ';')" />
    </xsl:element>
    </xsl:otherwise>
    </xsl:choose>

    <xsl:call-template name="TokenizeCommaSepString">
    <xsl:with-param name="SomeStringConcat" select="substring-after($SomeStringConcat,';')" />
    </xsl:call-template>
    </xsl:if>
    </xsl:template>

    By Blogger Pras Biswas, on November 01, 2006 9:57 PM  

Post a Comment