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

RE: counting the number of tokens in a string?

Subject: RE: counting the number of tokens in a string?
From: "Ben Robb" <Ben@xxxxxxxxxx>
Date: Wed, 2 Oct 2002 18:05:22 +0100
ben robb
Worse than a newbie question - a question that was only asked 24 hours
ago...

Mike Kay's response:
"Your first job is to tokenize this. Some processors (and EXSLT) have an
xx:tokenize() extension function that makes this easy; the only other
way is to use a recursive template (which isn't very difficult). Let's
say you end up with a variable $colours containing a node-set in which
there are four elements whose string values are blue, red, etc,"

My response:
If you aren't using a processor which supports tokenize() then use the
recursive method:

---------------------

<xsl:variable name="t">
	<xsl:call-template name="getTotal">
		<xsl:with-param name="s"
select="normalize-space(ITEM/@cols)"/>
		<xsl:with-param name="x" select="'1'"/>
	</xsl:call-template>
</xsl:variable>

<xsl:template name="getTotal">
	<xsl:param name="s"/>
	<xsl:param name="x"/>
	<xsl:choose>
		<xsl:when test="contains($s, ',')">
			<xsl:call-template name="getTotal">
				<xsl:with-param name="s"
select="substring-after($s,''')"/>
				<xsl:with-param name="x" select="$x +
1"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise><xsl:value-of
select="$x"/></xsl:otherwise>
	</xsl:choose>
</xsl:template>

---------------------

Then the total number of items in the cols attribute will be given by:

<xsl:value-of select="$t" />

Rgs,

Ben Robb


-----Original Message-----
From: john liao [mailto:jliao2k@xxxxxxxxx] 
Sent: 02 October 2002 17:36
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject:  counting the number of tokens in a string?


Hi,

another newbie question. Is there an easy way to count
the number of commas in a string? Like this:

<ITEM cols="col1,col2,col3,col4"/>

I would like to print 4 for the number of columns in
my ITEM. I searched on tokenize() but most of the
references are about the existence of it.

Thanks,

J.L.

=====


__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

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