Name

CountOccurrences

Original Contributor

Originally contributed by Matt Bramer (@IOnline247) and adapted by Marc D Anderson (@sympmarc).

Purpose

This template count the number of occurrences of findString within the withinString using recursive calls.

Example

<xsl:call-template name="CountOccurrences"> 
    <xsl:with-param name="withinString" select="@Title" /> 
    <xsl:with-param name="findString" select="'A'" /> 
</xsl:call-template> 

Code

<xsl:template name="CountOccurrences">
  <xsl:param name="withinString" />
  <xsl:param name="findString" />
  <xsl:param name="counter" select="0" />
  
  <xsl:choose>
   <xsl:when test="contains($withinString, $findString)">
    <xsl:call-template name="CountOccurrences">
     <xsl:with-param name="withinString" select="substring-after($withinString, $findString)" />
     <xsl:with-param name="findString" select="$findString" />
     <xsl:with-param name="counter" select="$counter + 1" />
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$counter" />
   </xsl:otherwise>
  </xsl:choose>

</xsl:template>

Last edited Jul 21, 2011 at 1:31 PM by sympmarc, version 4

Comments

No comments yet.