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:element>
    </xsl:template>
    <xsl:template match="a" >
        <xsl:call-template name="proc" >
            <xsl:with-param name="num" select="1" />
            <xsl:with-param name="txt" select="text()" />
        </xsl:call-template>
    </xsl:template>
    <xsl:template name="proc">
        <xsl:param name="num" select="0"/>
        <xsl:param name="txt" select="'" />
        <xsl:element name="{concat('a', $num)}" >
            <xsl:choose>
                <xsl:when test="contains($txt, '|')" >
                    <xsl:value-of select="substring-before($txt, '|')" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$txt" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:element>
        <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" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact