Search code examples
xsltxslt-1.0xslt-2.0tokenize

Splitting of strings based on the required length


Is there an easy way on how to split a string based from the required length? For example, I have a string:

<Data>AAAAABBBBB1111122222RRRRR<Data>

and I want to populate an output like this:

AAAAA
BBBBB
11111
22222
RRRRR

Thank you.


Solution

  • You can use analyze-string to break up the data:

    <xsl:template match="Data">
        <xsl:variable name="tokens" as="xs:string*">
            <xsl:analyze-string select="." regex=".{{1,5}}">
                <xsl:matching-substring>
                    <xsl:sequence select="."/>
                </xsl:matching-substring>
            </xsl:analyze-string>
        </xsl:variable>
        <xsl:value-of select="$tokens" separator="&#10;"/>
    </xsl:template>