How to convert string values such as “10,20,30,40″ into a XML node set dynamically.
The template
<xsl:template name="tokenize"> <xsl:param name="string" /> <xsl:param name="delim" /> <xsl:choose> <xsl:when test="contains($string, $delim)"> <token><xsl:value-of select="substring-before($string, $delim)" /></token> <xsl:call-template name="tokenize"> <xsl:with-param name="string" select="substring-after($string, $delim)" /> <xsl:with-param name="delim" select="$delim" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <token><xsl:value-of select="$string" /></token> </xsl:otherwise> </xsl:choose> </xsl:template> |
Usage
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"> <xsl:output method="xml"/> <xsl:template match="/"> <xsl:variable name="tokens"> <xsl:call-template name="tokenize"> <xsl:with-param name="string" select="'10,20,30,40,50'" /> <xsl:with-param name="delim" select="','" /> </xsl:call-template> </xsl:variable> <ul> <xsl:for-each select="exsl:node-set($tokens)/token"> <li><xsl:value-of select="."/></li> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet> |
Result
<ul> <li>10</li> <li>20</li> <li>30</li> <li>40</li> <li>50</li> </ul> |
