BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


XSLT
Randomization of node-sets or node-lists
XPath Analyzer
Finding the Maximum Date
World Cup 2002 fever hits XSLT!!
Using XSLT for DHTML Menus
RSS / RDF Newsfeed Reader
Hierarchical menu from Database + XSL to Html
XSLT syntax hightlighter
XSLT Stylesheet for listing namespaces used in a document
Rotate an SVG shape over it's center
Looping through each letter of the alphabet
An XSL Calculator using the FXSL Functional Programming Library
A Calendar, using the XSelerator's datetime library
Match a phrase to the closest sorted list value with 1 XPath Expression
SVG toolbar
Sort an XML document
Transposing rows into columns using XML/XSLT
XSLT ISAPI Filter for IIS
Browser-Based XML Editor
XSLT function for standardizing address values


 
 

<< XQuery.NET and XML >>


By Lee Humphries
First Posted 05/02/2002
Times viewed 138

Find the number of tokens or words in a string


Summary XPath's string functions lack a convenient means of doing things like extracting the third token in from the end of a list or even identifying how many words there are in a string. This code snippet does identify how many tokens there are in a list.

Although it's relatively easy to recurse through a list to identify the tokens within it. It becomes much more difficult when you want to process a list in reverse order, extract a token at a given position from the end, or even just perform a word count on a paragraph.

This code snippet is a fairly brute-force approach to counting tokens, but it eliminates any need for recursing through the list to achieve the count.

It features two methods, but both rely in the same trick of using the XPath Translate function to eliminate unwanted characters and then counting what's left behind.

One recommendation is that if you are doing word counts to normalize-space your input string and only use a single space character as your delimiter.

These methods will support an arbitrary number of single-character delimiters. However both may fail if the delimiter is a multi-character delimiter as any of the characters in the delimiter that also turn up in the tokens will be included in the processing this may result in an 'overcounting' of tokens.
The first method is also presently limited to eliminating English characters available from a standard keyboard.

Additional information

Further additional information


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< XQuery.NET and XML >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML