[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: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 17 Oct 2020 09:14:05 -0000
Re:  tokenizing and counting with xsl:analyze-string
You can construct a sequence of booleans, in which case you should use
<xsl:sequence select="true()"/> in place of <xsl:value-of select="1"/>, and
then you can use `count($temp_result[.])` and `count($temp_result[not(.)]` to
count the number of true and false items respectively.

If you want to construct the variable as a single string, you can use
xsl:value-of as I suggested, but then you must declare the variable
as="xs:string". But using a sequence of booleans is probably better.

Michael Kay

> On 17 Oct 2020, at 10:04, Mukul Gandhi gandhi.mukul@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> On Sat, Oct 17, 2020 at 1:22 PM Michael Kay mike@xxxxxxxxxxxx
<mailto:mike@xxxxxxxxxxxx> <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx
<mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>> wrote:
> With xsl:analyse-string you would still need a variable, but it could be a
simpler variable: for example it might just contain a "1" for a match, and a
"0" for a non-match; at the end you then need to count the ones and zeros
which you can do with string-length(translate(...)).
> With your suggestion, below mentioned is my new XSLT stylesheet,
> <xsl:stylesheet version="3.0"
>    <xsl:output method="xml" indent="yes"/>
>    <xsl:template match="/">
>       <xsl:variable name="temp_result" as="xs:boolean*">
>           <xsl:analyze-string
>                                          regex="hello[1-9]">
>              <xsl:matching-substring>
>                 <xsl:value-of select="1"/>
>              </xsl:matching-substring>
>              <xsl:non-matching-substring>
> 	        <xsl:value-of select="0"/>
>              </xsl:non-matching-substring>
>           </xsl:analyze-string>
>       </xsl:variable>
>       <result>
>          <yes count="{count(index-of($temp_result, true()))}"/>
>          <no count="{count(index-of($temp_result, false()))}"/>
>       </result>
>    </xsl:template>
> </xsl:stylesheet>
> The above stylesheet gives me the desired result.
> But the above mentioned XSLT stylesheet, doesn't do exactly what you've
> I would preferably, wish to declare my XSLT variable as follows,
> <xsl:variable name="temp_result" as="xs:string">
>     <xsl:analyze-string ...
> </xsl:variable>
> with an expectation that, content of this new kind of variable would be a
string (i.e, an atomic xs:string value) of 1 s & 0 s characters, on which I
could do string-length(translate(...)). Is this doable?
> --
> Regards,
> Mukul Gandhi
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <>)

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.