Re: finding word count within a document, with xsl:accumulator
| Newsgroups | gmane.text.xml.xsl.general.mulberrytech |
|---|---|
| Message-ID | <[email protected]> |
On Thu, Jan 21, 2021 at 5:36 PM Michael Kay [email protected] < [email protected]> wrote: > 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> > Thanks, Mike for the answer. Taking clue from your suggestion, I could write the following stylesheet (named wc_2.xsl), that works for me. <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:variable name="input-doc" as="document-node()"> <xsl:document> <xsl:value-of select="unparsed-text('inp1.txt')"/> </xsl:document> </xsl:variable> <xsl:accumulator name="w" initial-value="0" as="xs:integer"> <xsl:accumulator-rule match="$input-doc/text()" select="$value + count(tokenize(.))"/> </xsl:accumulator> <xsl:template match="/" mode="x1"> <xsl:variable name="wordCount" select="$input-doc/accumulator-after('w') - $input-doc/accumulator-before('w')"/> <xsl:value-of select="$wordCount"/> </xsl:template> </xsl:stylesheet> For above to work, I need to invoke Saxon (I'm using version 10.3) with following options, -s:wc_2.xsl -xsl:wc_2.xsl -im:x1 (i.e, I also have to specify an initial mode for the XSLT transformation) -- Regards, Mukul Gandhi --~---------------------------------------------------------------- XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/3329386 or by email: [email protected] --~--