XMetaL Tips and Tricks

XMetaL Community Forum XMetaL Tips and Tricks Script Example: Word Count Feature

  • Derek Read

    Script Example: Word Count Feature

    Participants 3
    Replies 4
    Last Activity 14 years, 1 month ago

    XMetaL Author 2.0+

    Script Example:
    This example adds a word count feature to XMetaL Author. The script counts the number of words inside PCDATA sections by placing the entire document in a string variable, removing all markup and counting all the “words”. A word is defined as a sequence of characters of any length not containing a whitespace.

    * Licensed Materials – Property of JustSystems, Canada, Inc.
    * (c) Copyright JustSystems Canada, Inc. 2008
    * All rights reserved.
    * The sample contained herein is provided to you “AS IS”.
    * It is furnished by JustSystems Corporation as a simple example and has not been
    * thoroughly tested under all conditions. JustSystems Canada, Inc., therefore, cannot
    * guarantee its reliability, serviceability or functionality.
    * This sample may include the names of individuals, companies, brands and products
    * in order to illustrate concepts as completely as possible. All of these names are
    * fictitious and any similarity to the names and addresses used by actual persons or
    * business enterprises is entirely coincidental.

    Script Installation and Usage:
    Important: This script is being made available for use by experienced users, administrators and/or IT departments (people that manage the installation and configuration of an XMetaL deployment). Test this script in a test environment (non-production machine) before distributing to end users. This is unsupported demo code.
       1. Download wordCountExample.zip.
       2. Unzip the file to an empty folder of your choice.
       3. Examine the code contained in the MCR file and integrate it into your XMetaL Author or XMAX customization. The MCR file has been written so that it may simply be dropped into the Startup folder to provide this functionality to all DTDs and Schemas (it is an Application-level macro).
       4. (optionally) Add a toolbar or menu item to run this script or modify the MCR file to include a shortcut key. If this is not done users can still run the script from the Macros toolbar. A BMP file containing an icon for this feature is also included. This file can be placed in the
    Icons folder. Modifications to enable this icon file will need to be made to the icons.ini file.



    Reply to: Script Example: Word Count Feature

    Nice short script. Of course, for DITA editing the contents of the prolog and related links sections may not be qualify as words. Counting title, shortdesc, and body is more appropriate. It is exactly this kind of small code that I am looking for in this forum!



    Reply to: Script Example: Word Count Feature

    Word-counting is one of those highly entertaining tasks for which 37 people will probably give you 37 different requirements.  This is an elegant and useful little bit of script, but it has problems.  For example, most of us would probably agree that the following bit of XML contains three words, but this script will say it has only one:

    • Äpfel
    • Bananen
    • Birnen

    Another approach to word-counting is to walk the DOM tree counting words per node.  Maybe I'll post a script like that here soon, unless Derek or someone else beats me to it…



    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. Here are some xslt snippets I've run across:




    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.


  • You must be logged in to reply to this topic.

Lost Your Password?