|
Subject: Re: [xsl] 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 |
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 |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| [xsl] tokenizing and counting with , Mukul Gandhi gandhi. | Thread | Re: [xsl] tokenizing and counting w, Michael Kay mike@xxx |
| [xsl] tokenizing and counting with , Mukul Gandhi gandhi. | Date | Re: [xsl] tokenizing and counting w, Michael Kay mike@xxx |
| Month |