Subject: [xsl] 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 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] minor improvements to XSL, Mukul Gandhi mukulg@ | Thread | Re: [xsl] finding word count within, Peter Flynn peter@xx |
Re: [xsl] minor improvements to XSL, Mukul Gandhi mukulg@ | Date | Re: [xsl] finding word count within, Peter Flynn peter@xx |
Month |