Re: [xsl] Count Words

Subject: Re: [xsl] Count Words
From: Dimtre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 9 Aug 2004 08:02:34 +1000
Use the str-split-to-words template from FXSL.

Search the Internet for "str-split-to-words" or see an example in the XSLT FAQ:

http://www.dpawson.co.uk/xsl/sect2/N1755.html#d2845e100


Cheers,

Dimitre Novatchev.


On Sun, 08 Aug 2004 15:50:20 -0600, M. David Peterson
<m.david@xxxxxxxxxx> wrote:
> 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";
>                 xmlns:exslt="http://exslt.org/common";
>                 version="1.0">
> <xsl:template match="poem">
> <xsl:variable name="wordCount">
>   <words>
>     <xsl:call-template name="wordCount">
>       <xsl:with-param name="paragraph"
> select="translate(normalize-space(hey_diddle), '.', '  ')"/>
>     </xsl:call-template>
>   </words>
> </xsl:variable>
> Words in Paragraph:
> <xsl:for-each select="exslt:node-set($wordCount)/words/word">
> Word <xsl:number/>: <xsl:value-of select="."/>
> </xsl:for-each>
> Total Words: <xsl:value-of select="count(exslt:node-set($wordCount)/words/word)"/>
> </xsl:template>
> <xsl:template name="wordCount">
> <xsl:param name="paragraph"/>
> <xsl:if test="$paragraph">
>   <word>
>     <xsl:value-of select="substring-before($paragraph, ' ')"/>
>   </word>
>   <xsl:call-template name="wordCount">
>     <xsl:with-param name="paragraph" select="substring-after($paragraph, ' ')"/>
>   </xsl:call-template>
> </xsl:if>
> </xsl:template>
> </xsl:stylesheet>
> 
> 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!
> 
> <M:D/>
> 
> 
> 
> Karl J. Stubsjoen wrote:
> 
> > 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

Current Thread