|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: Count Words
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 = "'
'"/> <xsl:with-param name = "replace-with" select = "' '"/> </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 = "' '"/> </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 = "' '"/> </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 = "' '"/> </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 = "' '"/> </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 = "'  '"/> <xsl:with-param name = "replace-with" select = "' '"/> </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,' ')"> <xsl:variable name = "new-text" select = "substring-after($text,' ')"/> <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: Count Words Date: Sun, 8 Aug 2004 13:18:41 -0700
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








