Subject: [xsl] tokenizing and counting with xsl:analyze-string From: "Mukul Gandhi gandhi.mukul@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Sat, 17 Oct 2020 07:29:20 -0000 |
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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[no subject], Unknown | Thread | Re: [xsl] tokenizing and counting w, Willem Van Lishout w |
[no subject], Unknown | Date | Re: [xsl] tokenizing and counting w, Willem Van Lishout w |
Month |