PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Calling a recursive template instead of a Split String function in XSLT

Posted by PANVEGA on January 15, 2009

When I wanted to try to access a multiple Lookup field the values are seperated in one String like this:

one; two; three; four; five  etc.

In the next step I wanted to write the values in seperate fields and add the values for URL Encoding parameters. The best way to accessing the Lookup String is to use a XSL recursive solution.I have to use this way, because in XSLT you do not have a split string function to write each value in an array.

Here is an example:

I have created an XSLT template that parses the standard ICS format and transforms it into XHTML. Here is the code that parses the properties of the ICS file into a list of properties that can be displayed in XHTML. Feed the $ical-data-raw parameter with ICS data and you will get XHTML formatted properties.

Call the template for the first time:

<xsl:call-template name="lookupString">
     <xsl:with-param name="input" select="concat($stringValue,';')"/>               
</xsl:call-template>

Recursive template calls:

<xsl:template name="lookupString">
    <xsl:param name="input" select="$input-data"/>
    <xsl:param name="value" select="substring-before($input,';')"/>
    <xsl:param name="next-string" select="substring-after($input,';')"/>

    <xsl:if test="$next-string">
        <xsl:call-template name="lookupString">
            <xsl:with-param name="input" select="$next-string"/>
        </xsl:call-template>
    </xsl:if>
    
   <a>
    <xsl:attribute name="href">
        /newpage.aspx?Check=<xsl:value-of select="$value"/>
    </xsl:attribute>
    </a>
    
</xsl:template>

In the recursive template  I have to check if the string ist not finished, otherwise you can get a stack overflow.

The function substring-before() returns a part of the string provided as the first argument which occurs before the first occurrence of the substring given in the second argument. The function substring-after() returns the part following the first occurrence of the substring. If the substring does not occur in the original string and empty string is returned.

Good luck!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: