xsl-list
[Top] [All Lists]

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

2021-01-21 23:24:33
On Thu, Jan 21, 2021 at 5:36 PM Michael Kay mike(_at_)saxonica(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>