My ramblings on just about everything...

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

Yet another very old post from my previous blog.

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.

    1 <xsl:template name="TokenizeCommaSepString">

    2     <xsl:param name="stringToTokenize" />

    3     <xsl:param name="destinationElementName" />

    4     <xsl:if test="$stringToTokenize != ''">

    5         <xsl:choose>

    6             <xsl:when test="contains($stringToTokenize, ',')=0">

    7                 <xsl:element name="{$destinationElementName}">

    8                     <xsl:value-of select="$stringToTokenize" />

    9                 </xsl:element>

   10             </xsl:when>

   11             <xsl:otherwise>

   12                 <xsl:element name="{$destinationElementName}">

   13                     <xsl:value-of select="substring-before($stringToTokenize, ',')" />

   14                 </xsl:element>

   15             </xsl:otherwise>

   16         </xsl:choose>

   17         <xsl:call-template name="TokenizeCommaSepString">

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

   19             <xsl:with-param name="destinationElementName" select="$destinationElementName" />

   20         </xsl:call-template>

   21     </xsl:if>

   22 </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.


Feedback

# re: Using recursion in the In-line XSLT Template type Scripting Functoid...

Thank you very much for the example. I think it's the first time I see something coming out of an Inline XSLT Call Template in my map ;)

But I have a problem with my version of your XSLT: it just generates 1 output node when it should generate more :( Any idea?

Thank you again !! 8/19/2005 4:19 AM | Paula

# re: Using recursion in the In-line XSLT Template type Scripting Functoid...

If you pass an input string like:

red,blue,green,yellow

and the destination Elelment Name thats passed is "Color":

then your resultant xml should look like
<Color>red</Color>
<Color>blue</Color>
<Color>green</Color>
<Color>yellow</Color> 8/19/2005 10:22 AM | Dev

# re: Using recursion in the In-line XSLT Template type Scripting Functoid...

Thank you for your reply.

Now it works. It was a problem with a looping functoid in a second transformation :) 8/22/2005 1:15 AM | Paula

# re: Using recursion in the In-line XSLT Template type Scripting Functoid...

Nice one, almost exactly the same as I am using.

What I want to know is can you reuse this template if you need to perform this function several times?

2/1/2006 2:14 AM | John

# re: Using recursion in the In-line XSLT Template type Scripting Functoid...

Sure thing, I'd hoped someone eventually would. :) 2/1/2006 8:14 PM | dev

# re: Using recursion in the In-line XSLT Template type Scripting Functoid...

Hi
I am not able to use this example can you please send me an example for using this functiod

6/20/2006 10:08 PM | Simit

# re: Using recursion in the In-line XSLT Template type Scripting Functoid...

Hi,

i have nearly the same problem - i must split a large text field into portions of 80 characters.

i'm an absolut newbie on xslt - so please can you help me ?

thx,
Markus 6/21/2006 9:21 AM | Markus





 

Please add 1 and 3 and type the answer here: