How do I use XLST to parse through a delimited string value?

Roseanne Zhang

It is not easy, but can be done. It also requires recursion. You probably can do better than I did, but here is a solution anyway. It might not be the greatest, but it works.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="/*" >
        <xsl:element name="{name()}">
            <xsl:apply-templates />
    <xsl:template match="a" >
        <xsl:call-template name="proc" >
            <xsl:with-param name="num" select="1" />
            <xsl:with-param name="txt" select="text()" />
    <xsl:template name="proc">
        <xsl:param name="num" select="0"/>
        <xsl:param name="txt" select="'" />
        <xsl:element name="{concat('a', $num)}" >
                <xsl:when test="contains($txt, '|')" >
                    <xsl:value-of select="substring-before($txt, '|')" />
                    <xsl:value-of select="$txt" />
        <xsl:variable name="left" select="substring-after($txt, '|')" />
        <xsl:if test="string-length($left)>1" >
            <xsl:call-template name="proc" >
                <xsl:with-param name="num" select="$num + 1" />
                <xsl:with-param name="txt" select="$left" />