Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Way to count # of instances of string in a string

Status
Not open for further replies.

acsooley

Programmer
Nov 13, 2002
32
0
0
CA
I would like to count the # of instances of a string in a string to return a number. Is there a way to do this in XSL?

var adam=
adamsooleywasheretoaskaquestionaboutxmladamwouldlikearesponse

count(contains($var, "adam")

returns 2
 
Suppose the data string is stored in certain element like this.

[tt]<data>adamsooleywasheretoaskaquestionaboutxmladamwouldlikearesponse</data>
[/tt]
The way to do it is a standard recursion. Here is a version I sketch---maybe not the best.
[tt]
<xsl:template match="//data">
<xsl:call-template name="substr_count">
<xsl:with-param name="str" select="." />
<xsl:with-param name="sub" select="'adam'" />
<xsl:with-param name="cnt" select="0" />
</xsl:call-template>
<xsl:template name="substr_count">
<xsl:param name="str" />
<xsl:param name="sub" />
<xsl:param name="cnt" />
<xsl:choose>
<xsl:when test="contains($str,$sub)">
<xsl:call-template name="substr_count">
<xsl:with-param name="str" select="substring-after($str,$sub)" />
<xsl:with-param name="sub" select="$sub" />
<xsl:with-param name="cnt" select="$cnt+1" />
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:text>[</xsl:text>
<xsl:value-of select="$cnt" />
<xsl:text>]</xsl:text>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
</xsl:template>
[/tt]
Between xsl:text are for cosmetic, inside it shows the result.

ps: How much study you do on your own? That looks very like assignment though often not obvious at the beginning.
 
I can assure you it is not an assignment. I and creating a online task list for work. Have everything working fine except when I display the results say on one user, I get he has done 12 tasks. (Meeting, Web, Course, Ticket, Ticket, Ticket, Web, Meeting, Meeting, Web, Course) each have a time amount for each task. But when I want it to display:
------------------------------------
Meeting - Total time: 2 hours
Web - Total time: 7 hours
Course - Total time: 1 hours
Ticket - Total time: 8 hours
------------------------------------

I get:
------------------------------------
Meeting - Total time: 2 hours
Web - Total time: 7 hours
Course - Total time: 1 hours
Ticket - Total time: 8 hours
Ticket - Total time: 8 hours
Ticket - Total time: 8 hours
Web - Total time: 7 hours
Meeting - Total time: 2 hours
Meeting - Total time: 2 hours
Web - Total time: 7 hours
Course - Total time: 1 hours
------------------------------------

So I get the totals fine but it repeats all occurnces of task with the total time instead of stopping.

So bacisally I am trying anything I can think of for it to display only one.

Thanks for your help though.

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top