Re: [xsl] tokenizing and counting with xsl:analyze-string

Subject: Re: [xsl] tokenizing and counting with xsl:analyze-string
From: "Mukul Gandhi gandhi.mukul@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 17 Oct 2020 10:38:41 -0000
On Sat, Oct 17, 2020 at 2:44 PM Michael Kay mike@xxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> 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

Current Thread