Re: [xsl] finding word count within a document, with xsl:accumulator

Subject: Re: [xsl] finding word count within a document, with xsl:accumulator
From: "Mukul Gandhi mukulg@xxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 22 Jan 2021 05:24:28 -0000
On Thu, Jan 21, 2021 at 5:36 PM Michael Kay mike@xxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> 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

Current Thread