|
Subject: RE: [xsl] Count Words From: xptm@xxxxxxx Date: Wed, 11 Aug 2004 14:49:48 +0100 |
I've done something like this to count words (courtesy of somebody from this
list):
<xsl:variable name="sql"><xsl:value-of select="RowSource"
/></xsl:variable>
<xsl:variable name="x" select="normalize-space($sql)" />
<xsl:variable name="y" select="translate($sql, ' ', '')" />
<xsl:variable name="wc" select="string-length($x) -
string-length($y) +1" />
wc = word count.
Quoting Vasu Chakkera <vasucv@xxxxxxxxxxx>:
> 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,substr
ing-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: [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
>
>
O SAPO ja esta livre de vmrus com a Panda Software, fique vocj tambim!
Clique em: http://antivirus.sapo.pt
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| RE: [xsl] Count Words, Vasu Chakkera | Thread | [xsl] splittingNode-set2, mankar |
| Re: [xsl] XSLT: check if value is w, xptm | Date | Re: [xsl] XSLT: check if value is w, M. David Peterson |
| Month |