[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: finding word count within a document, with xsl:ac
You would need to wrap the string in a document: <xsl:variable name="input-doc" as="document-node()"> <xsl:document> <xsl:value-of select="unparsed-text('input.txt')"/> </xsl:document> </xsl:variable> However this is pretty convoluted. If you define a function word-count($string) then you can (a) use this in your accumulator, and (b) apply it to a string directly, without any need for accumulators or document nodes. Personally I wouldn't use accumulators for this unless (a) you intend to use streaming, or (b) there is a need to get the wordcount for arbitrary parts of the document, e.g. chapters or sections. Michael Kay Saxonica > On 21 Jan 2021, at 11:04, Mukul Gandhi mukulg@xxxxxxxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote: > > 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 <http://www.w3.org/1999/XSL/Transform>" > xmlns:xs="http://www.w3.org/2001/XMLSchema <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 > XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list> > EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by email <>)
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|