[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Counting Word Occurences

Subject: Counting Word Occurences
From: "Kirk V. Hastings" <kvh2n@xxxxxxxxxxxx>
Date: Tue, 19 Dec 2000 11:19:19 -0500
occurence word
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.