Home Forums XMetaL Tips and Tricks Script Example: Word Count Feature Reply To: Script Example: Word Count Feature

Derek Read

Reply to: Script Example: Word Count Feature

I think in the case of xml content, this is a task I would put in the xslts rather than the editor.

The requirement this script attempts to help with is the need some people have to count the number of words in the document they are currently writing (for whatever reason) at any point without needing to save or do any kind of transform. This provides one solution that is extremely easily to integrate as it is done purely via script and relies completely on JScript with no need for any external parsing engines. Here are some benefits to this method:

1. Script is very simple and easy to understand.
2. Script runs very fast because it uses pure string manipulation methods built into JScript engine and does not require interaction with an external control (such as XSLT engine) or XMetaL Author
APIs (which would be needed for a DOM manipulation solution).
3. Works the same for all schema (due to the simplistic definition of what a “word” is). Some would argue this is exactly where the script falls down (it really depends on your needs).

Those things having been said, if anyone else can improve on what's been posted here and make it easily integrated into XMetaL Author I'm sure others would like to see what you've done. In future examples I will try to clearly state what problem an example is trying to solve to make things clear.

The most obvious limitation with this script is the simplified definition it uses for “word”. For the vast majority of people that just want a rough estimate this script is likely good enough. As others have stated though, using XSLT or DOM calls could be more accurate, though that would depend on your needs as well as the schema you are using. I don't think a truly accurate solution can be made to work for all schema though, and in many cases (as hinted at by mag3737) building an intimate knowledge of a particular schema into the solution may be required to provide a truly accurate word count, and that means such a solution will not work for all schema.