Re: tokenizing and counting with xsl:analyze-string

"Mukul Gandhi [email protected]" <[email protected]>
Newsgroups gmane.text.xml.xsl.general.mulberrytech
Message-ID <[email protected]>
On Sat, Oct 17, 2020 at 2:44 PM Michael Kay [email protected] <
[email protected]> wrote:

> 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.
>

Thanks. I'm happy with this.


> 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".
>

Looks like, its not possible to write the XSLT solution for my use case
with this way. Below is my attempt to trying do this,

<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="xml" indent="yes"/>

   <xsl:template match="/">
      <xsl:variable name="temp_result" as="xs:string">
          <xsl:analyze-string
select="'abhello1cdehello2fghijklhello3hello4mhello5nhello6'"
                              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>
         <xsl:value-of select="count($temp_result)"/>
      </result>
   </xsl:template>

</xsl:stylesheet>

With above, the XSLT 3.0 processor presents me with following error,

Type error at char 0 in expression in xsl:analyze-string/@select on line 10
column 50 of anl_str_2.xsl:
  XTTE0570  A sequence of more than one item is not allowed as the value of
variable
  $temp_result ("0", "1")
  In template rule with match="/" on line 7 of anl_str_2.xsl
A sequence of more than one item is not allowed as the value of variable
$temp_result ("0", "1")

I think that, as per XSLT 3.0 spec, the child content model
of xsl:matching-substring / xsl:non-matching-substring is a sequence
constructor, which would not allow xsl:analyze-string to return an atomic
value (except in a case, when the returned sequence's size is one).



-- 
Regards,
Mukul Gandhi
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/3329386
or by email: [email protected]
--~--
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.