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

finding word count within a document, with xsl:accumu

Subject: finding word count within a document, with xsl:accumulator
From: "Mukul Gandhi mukulg@xxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 21 Jan 2021 11:03:41 -0000
 finding word count within a document
Hi all,
   I'm exploring ways with XSLT 3.0's xsl:accumulator instruction in a non
streaming way, to count number of words within text documents (both XML and
.txt input files).

Below is what I've tried so far with input XML documents, using examples
borrowed from XSLT 3.0 spec.

XML input document:

<document>
   Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum
   has been the industry's standard dummy text ever since the 1500s, when
an unknown
   printer took a galley of type and scrambled it to make a type specimen
book. It has
   survived not only five centuries, but also the leap into electronic
typesetting, remaining
   essentially unchanged. It was popularised in the 1960s with the release
of Letraset sheets
   containing Lorem Ipsum passages, and more recently with desktop
publishing software like
   Aldus PageMaker including versions of Lorem Ipsum.
</document>

I wish to find, the word count within the XML element "document" above.

XSLT stylesheet:

<xsl:stylesheet version="3.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
                                               xmlns:xs="
http://www.w3.org/2001/XMLSchema"
                                               exclude-result-prefixes="xs">

   <xsl:output method="text"/>

   <xsl:mode use-accumulators="w"/>

   <xsl:accumulator name="w" initial-value="0" as="xs:integer">
      <xsl:accumulator-rule match="text()"
                            select="$value + count(tokenize(.))"/>
   </xsl:accumulator>

   <xsl:template match="document">
     <xsl:variable name="wordCount" select="accumulator-after('w') -
accumulator-before('w')"/>
     <xsl:value-of select="$wordCount"/>
   </xsl:template>

</xsl:stylesheet>

When the above XSLT stylesheet, is applied to the earlier mentioned XML
input document, the word count is correctly displayed as output of XSLT
transformation (answer is 91 with the above example).

If I've a text input document (say a file inp1.txt), as following (italics
is just to emphasize, what is within the .txt file),

*Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum *
*has been the industry's standard dummy text ever since the 1500s, when an
unknown *
*printer took a galley of type and scrambled it to make a type specimen
book. It has *
*survived not only five centuries, but also the leap into electronic
typesetting, remaining*
*essentially unchanged. It was popularised in the 1960s with the release of
Letraset sheets *
*containing Lorem Ipsum passages, and more recently with desktop publishing
software like *
*Aldus PageMaker including versions of Lorem Ipsum.*

How would an XSLT 3.0 transform using xsl:accumulator, needs to be written,
to count number of words with the mentioned .txt file?

Thanks for any replies.



-- 
Regards,
Mukul Gandhi

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.