xsl-list
[Top] [All Lists]

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

2020-10-17 02:52:18
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(...)).

Michael Kay
Saxonica

On 17 Oct 2020, at 08:29, Mukul Gandhi 
gandhi(_dot_)mukul(_at_)gmail(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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 
<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/293509> (by 
email <>)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>