[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

tokenizing and counting with xsl:analyze-string

Subject: tokenizing and counting with xsl:analyze-string
From: "Mukul Gandhi gandhi.mukul@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 17 Oct 2020 07:29:20 -0000
 tokenizing and counting with xsl:analyze-string
Hi all,
   I'm mentioning my XSLT use case as follows,

I've got an input string. I also have a regex, which may match words within
an input string. When traversing an input string from left to right, I wish
to count how many words according to the regex appeared within an input
string, and how many words did not match the regex within the input string.

My desired XML output with this XSLT program, shall be as follows,

   <yes count="6"/>
   <no count="5"/>

(within the result, value of attribute count would be a number mentioning
how many words matched [shown with element 'yes'] and how many words did
not match [shown with element 'no'] with the regex)

I've tried to solve above use case, using xsl:analyze-string instruction.
Below is my XSLT stylesheet for this,

<xsl:stylesheet version="3.0" xmlns:xsl="

   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/">
      <xsl:variable name="temp_result" as="element()*">
                <yes><xsl:value-of select="."/></yes>
        <no><xsl:value-of select="."/></no>
         <yes count="{count($temp_result/self::yes)}"/>
         <no count="{count($temp_result/self::no)}"/>


(my input string is value of xsl:analyze-string's 'select' attribute)

When I run the above XSLT stylesheet, I get the desired output.

My questions are as follows,
I'm wondering, is the above mentioned XSLT stylesheet, the kind of only
algorithmic way (i.e using, xsl:analyze-string) to solve the mentioned use
case? Within an XSLT solution for this use case, I wish to avoid probably,
using a variable to store intermediate result ($temp_result within the
above mentioned XSLT stylesheet) before computing the desired counts from
this populated variable.

Any thoughts please.

Mukul Gandhi

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.