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)
The answer you saw is only missing a delimiter parameter:
I. XSLT 2.0 solution
when this transformation is applied on ant XML document (not used), the wanted, correct result is produced:
Explanation: Use of the standard XPath 2.0 function
tokenize()
.II. XSLT 1.0: Using the FXSL 1.x
str-split-to-words
function/templateThis XSLT 1.0 transformation:
when applied on this XML document:
produces the wanted, correct result:
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:
when applied on this XML document:
produces the wanted, correct result:
IV. Passing to the
tokenize
template in III above as a parameter a template/function to process each tokenwhen this transformation is applied to the same XML document as in III, the same, wanted, correct result is produced:
Now, if we substitute the last template with this one:
again the desired result is produced: