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

Re: Count Words

Subject: Re: Count Words
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Sun, 08 Aug 2004 15:50:20 -0600
diddle word
Hey Karl,

I would use a recursive named template as such (notice the use of the normalize-space and the translate functions. The first to get rid of leading, trailing, and extra spaces between words and the second to convert any periods into spaces so that the named template properly processes the final word of each sentence):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:template match="poem">
<xsl:variable name="wordCount">
<xsl:call-template name="wordCount">
<xsl:with-param name="paragraph" select="translate(normalize-space(hey_diddle), '.', ' ')"/>
Words in Paragraph:
<xsl:for-each select="exslt:node-set($wordCount)/words/word">
Word <xsl:number/>: <xsl:value-of select="."/>
Total Words: <xsl:value-of select="count(exslt:node-set($wordCount)/words/word)"/>
<xsl:template name="wordCount">
<xsl:param name="paragraph"/>
<xsl:if test="$paragraph">
<xsl:value-of select="substring-before($paragraph, ' ')"/>
<xsl:call-template name="wordCount">
<xsl:with-param name="paragraph" select="substring-after($paragraph, ' ')"/>

When applied to your XML the result is:

<?xml version="1.0" encoding="utf-8"?>
Words in Paragraph:

Word 1: Hey
Word 2: diddle
Word 3: diddle,
Word 4: the
Word 5: cat
Word 6: and
Word 7: the
Word 8: fiddle,
Word 9: The
Word 10: cow
Word 11: jumped
Word 12: over
Word 13: the
Word 14: moon,
Word 15: The
Word 16: little
Word 17: dog
Word 18: laughed
Word 19: to
Word 20: see
Word 21: such
Word 22: sport,
Word 23: And
Word 24: the
Word 25: dish
Word 26: ran
Word 27: away
Word 28: with
Word 29: the
Word 30: spoon
Total Words: 30

Best of luck!


Karl J. Stubsjoen wrote:


Given the following:

=========== xml source ==========
	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.
=========== /xml source ==========

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


Current Thread


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.
First Name
Last Name
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.