I would like to implement a split function/template
in XSLT which takes as input a string and a delimiter and returns a split array of the string..
Or rather I'd like the ability to do something along the lines of:
<xsl:call-template name="F">
<xsl:with-param name="input" select="'a,b,c,d,e'"/>
<xsl:with-param name="replacement">
<option value='$x'>$x</option>
</xsl:with-param>
</xsl:call-template>
which will give me
<option value='a'>a</option><option value='b'>b</option><option value='c'>c</option><option value='d'>d</option><option value='e'>e</option>
Question targeted at XSLT 1.0 (but i don't mind learning how XSLT 2.0 does it too)
I. XSLT 2.0 solution
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vStr" select="'a,b,c,d,e'"/>
<xsl:for-each select="tokenize($vStr, ',')">
<option value="{.}"><xsl:value-of select="."/></option>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on ant XML document (not used), the wanted, correct result is produced:
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
Explanation: Use of the standard XPath 2.0 function tokenize()
.
II. XSLT 1.0: Using the FXSL 1.x str-split-to-words
function/template
This XSLT 1.0 transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="', '"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select=
"ext:node-set($vwordNodes)/*"/>
</xsl:template>
<xsl:template match="word">
<option value="{.}"><xsl:value-of select="."/></option>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<t>a,b,c d,e</t>
produces the wanted, correct result:
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
Do note The pDelimiters
parameter (as its name says) can hold more than one delimiting character -- in this case we use both ','
and ' '
.
III. XSLT 1.0 solution using hand-written recursive named template:
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="text()" name="tokenize">
<xsl:param name="pText" select="."/>
<xsl:param name="pDelim" select="','"/>
<xsl:if test="string-length($pText) > 0">
<xsl:variable name="vToken" select=
"substring-before(concat($pText,','), ',')"/>
<option value="{$vToken}">
<xsl:value-of select="$vToken"/>
</option>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select=
"substring-after($pText,',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<t>a,b,c,d,e</t>
produces the wanted, correct result:
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
IV. Passing to the tokenize
template in III above as a parameter a template/function to process each token
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net" exclude-result-prefixes="f">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<f:processToken/>
<xsl:variable name="vFunc" select=
"document('')/*/f:processToken[1]"/>
<xsl:template match="text()" name="tokenize">
<xsl:param name="pText" select="."/>
<xsl:param name="pDelim" select="','"/>
<xsl:param name="pProcessFunc" select="$vFunc"/>
<xsl:if test="string-length($pText) > 0">
<xsl:variable name="vToken" select=
"substring-before(concat($pText,','), ',')"/>
<xsl:apply-templates select="$pProcessFunc">
<xsl:with-param name="arg1" select="$vToken"/>
</xsl:apply-templates>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select=
"substring-after($pText,',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="f:processToken">
<xsl:param name="arg1"/>
<option value="{$arg1}">
<xsl:value-of select="$arg1"/>
</option>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied to the same XML document as in III, the same, wanted, correct result is produced:
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
Now, if we substitute the last template with this one:
<xsl:template match="f:processToken">
<xsl:param name="arg1"/>
<p>
<xsl:value-of select="$arg1"/>
</p>
</xsl:template>
again the desired result is produced:
<p>a</p>
<p>b</p>
<p>c</p>
<p>d</p>
<p>e</p>
The answer you saw is only missing a delimiter parameter:
<xsl:template name="output-tokens">
<xsl:param name="list" />
<xsl:param name="delimiter" />
<xsl:variable name="newlist">
<xsl:choose>
<xsl:when test="contains($list, $delimiter)"><xsl:value-of select="normalize-space($list)" /></xsl:when>
<xsl:otherwise><xsl:value-of select="concat(normalize-space($list), $delimiter)"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="first" select="substring-before($newlist, $delimiter)" />
<xsl:variable name="remaining" select="substring-after($newlist, $delimiter)" />
<xsl:value-of select="$first" /> <!-- token -->
<xsl:if test="$remaining">
<xsl:call-template name="output-tokens">
<xsl:with-param name="list" select="$remaining" />
<xsl:with-param name="delimiter"><xsl:value-of select="$delimiter"/></xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>