RE: [xsl] Count Words

Subject: RE: [xsl] Count Words
From: "Vasu Chakkera" <vasucv@xxxxxxxxxxx>
Date: Wed, 11 Aug 2004 13:40:29 +0000
Try this
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template match="/">
<xsl:call-template name = "find-words-count">
<xsl:with-param name = "text" select = "/poem/hey_diddle"/>
</xsl:call-template>


</xsl:template>
<xsl:template name="find-words-count">
<xsl:param name="text"/>
<xsl:value-of select = "$text"/>
<hr/>
<xsl:variable name = "text-without-punctuations">
<xsl:call-template name = "remove-punctuations">
<xsl:with-param name = "text" select = "$text"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="count-words">
<xsl:with-param name="text" select="$text-without-punctuations"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="text-string"/>
<xsl:param name="find-word"/>
<xsl:param name="replace-with"/>
<xsl:choose>
<xsl:when test="contains($text-string,$find-word)">
<xsl:call-template name="replace">
<xsl:with-param name="text-string"
select="concat(substring-before($text-string,$find-word),$replace-with,substring-after($text-string,$find-word))"/>
<xsl:with-param name="find-word" select="$find-word"/>
<xsl:with-param name="replace-with" select="$replace-with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text-string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="remove-punctuations">
<xsl:param name="text"/>
<xsl:variable name = "remove-newlines">
<xsl:call-template name = "replace">
<xsl:with-param name = "text-string" select = "$text"/>
<xsl:with-param name = "find-word" select = "'&#xa;'"/>
<xsl:with-param name = "replace-with" select = "'&#x20;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name = "remove-fullstops">
<xsl:call-template name = "replace">
<xsl:with-param name = "text-string" select = "$remove-newlines"/>
<xsl:with-param name = "find-word" select = "'.'"/>
<xsl:with-param name = "replace-with" select = "'&#x20;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name = "remove-commas">
<xsl:call-template name = "replace">
<xsl:with-param name = "text-string" select = "$remove-fullstops"/>
<xsl:with-param name = "find-word" select = "','"/>
<xsl:with-param name = "replace-with" select = "'&#x20;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name = "remove-question-marks">
<xsl:call-template name = "replace">
<xsl:with-param name = "text-string" select = "$remove-commas"/>
<xsl:with-param name = "find-word" select = "'?'"/>
<xsl:with-param name = "replace-with" select = "'&#x20;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name = "remove-excl-marks">
<xsl:call-template name = "replace">
<xsl:with-param name = "text-string" select = "$remove-question-marks"/>
<xsl:with-param name = "find-word" select = "'!'"/>
<xsl:with-param name = "replace-with" select = "'&#x20;'"/>
</xsl:call-template>
</xsl:variable>


<xsl:variable name = "remove-multiple-spaces">
<xsl:call-template name = "replace">
<xsl:with-param name = "text-string" select = "$remove-excl-marks"/>
<xsl:with-param name = "find-word" select = "'&#x20;&#x20;'"/>
<xsl:with-param name = "replace-with" select = "'&#x20;'"/>
</xsl:call-template>
</xsl:variable>
<hr/>
<xsl:value-of select = "normalize-space($remove-multiple-delims)"/>
</xsl:template>
<xsl:template name="count-words">
<xsl:param name="text"/>
<xsl:param name="count" select="1"/>
<xsl:choose>
<xsl:when test="contains($text,'&#x20;')">
<xsl:variable name = "new-text" select = "substring-after($text,'&#x20;')"/>
<xsl:call-template name = "count-words">
<xsl:with-param name = "text" select = "$new-text"/>
<xsl:with-param name="count" select="$count+1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>


               <xsl:value-of select = "$count"/>
           </xsl:otherwise>
       </xsl:choose>
   </xsl:template>
</xsl:stylesheet>


You could store every template defined above except the first one in a file called utils.xsl and use it.
Hope this helps
Vasu


From: "Karl J. Stubsjoen" <karl@xxxxxxxxxxxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: [xsl] Count Words
Date: Sun, 8 Aug 2004 13:18:41 -0700

Hello,

Given the following:

=========== xml source ==========
<poem>
  <hey_diddle>
	Hey diddle diddle, the cat and the fiddle,
	The cow jumped over the moon,
	The little dog laughed to see such sport,
	And the dish ran away with the spoon.
  </hey_diddle>
</poem>
=========== /xml source ==========

How do I count how many words are contained within the node <hey_diddle/>?

Karl


_________________________________________________________________ Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo

Current Thread