This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Counting Word Occurences
- To: XSL-List at lists dot mulberrytech dot com
- Subject: [xsl] Counting Word Occurences
- From: "Kirk V. Hastings" <kvh2n at virginia dot edu>
- Date: Tue, 19 Dec 2000 11:19:19 -0500
- Reply-To: xsl-list at lists dot mulberrytech dot com
In an attempt to implement simple keyword searching, I did the following to
count the number of occurences of the user's keyword in the <abstract>
element. What I would like to do now is count the number of times that the
keyword occurs in the context node's child <abstract> as well as all
descendant <div> element's <abstract> children. I was hoping it would be as
simple as defining the variable "text" as
"descendant-or-self::div/abstract", but that only gave me the count for the
first <abstract> element. I think what I'm trying to do is concatenate the
text of all the descendant <abstract> elements into a single string. Or am I?
<doc>
<div>
<title></title>
<abstract\>foo bar</abstract>
<div>
<title></title>
<abstract\>foo bar</abstract>
</div>
</div>
</doc>
<xsl:param name="keyword" select="foo"/>
<xsl:template match="div" mode="toc">
<xsl:variable name="text" select="div/abstract"/>
<xsl:if test="contains($text, $keyword)">
<xsl:call-template name="count">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="keyword" select="$keyword"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="count">
<xsl:param name="text"/>
<xsl:param name="keyword"/>
<xsl:choose>
<xsl:when test="contains($text, $keyword)">
<xsl:variable name="occurence" select="substring-before($text,
$keyword)"/>
<xsl:variable name="remainder" select="substring-after($text,
$keyword)"/>
<xsl:variable name="total">
<xsl:call-template name="count">
<xsl:with-param name="text" select="$remainder"/>
<xsl:with-param name="keyword" select="$keyword"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$total + '1'"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:template>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list