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

Re: tokenizing and counting with xsl:analyze-string

Subject: Re: tokenizing and counting with xsl:analyze-string
From: "Willem Van Lishout willemvanlishout@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 17 Oct 2020 07:50:03 -0000
Re:  tokenizing and counting with xsl:analyze-string
You can use the analyze-string function to return the matched and
non-matched substrings, and then do a simple count.

<xsl:variable name="result"
select="analyze-string('abhello1cdehello2fghijklhello3hello4mhello5nhello6',
'hello[1-9]')"/>
      <result>
         <yes><xsl:sequence select="$result/*:match => count()"/></yes>
         <no><xsl:sequence select="$result/*:non-match => count()"/></no>
      </result>
</xsl:variable>

Willem Van Lishout
willemvanlishout@xxxxxxxxx



On Sat, Oct 17, 2020 at 9:29 AM Mukul Gandhi gandhi.mukul@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> 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,
>
> <result>
>    <yes count="6"/>
>    <no count="5"/>
> </result>
>
> (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="
> http://www.w3.org/1999/XSL/Transform">
>
>    <xsl:output method="xml" indent="yes"/>
>
>    <xsl:template match="/">
>       <xsl:variable name="temp_result" as="element()*">
>           <xsl:analyze-string
> select="'abhello1cdehello2fghijklhello3hello4mhello5nhello6'"
>                                          regex="hello[1-9]">
>              <xsl:matching-substring>
>                 <yes><xsl:value-of select="."/></yes>
>              </xsl:matching-substring>
>              <xsl:non-matching-substring>
>         <no><xsl:value-of select="."/></no>
>              </xsl:non-matching-substring>
>           </xsl:analyze-string>
>       </xsl:variable>
>       <result>
>          <yes count="{count($temp_result/self::yes)}"/>
>          <no count="{count($temp_result/self::no)}"/>
>       </result>
>    </xsl:template>
>
> </xsl:stylesheet>
>
> (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.
>
>
>
> --
> Regards,
> Mukul Gandhi
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/3166594> (by
> email <>)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

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.
Email
First Name
Last Name
Company
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.