|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Count Words
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,
|
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








