split function in xslt 1.0

I. XSLT 1.0 solution:

Here is one way to do this in XSLT 1.0 using only the xxx:node-set() extension function:

<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:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="mark">
  <xsl:variable name="vrtfSplit">
   <xsl:apply-templates/>
  </xsl:variable>

  <xsl:for-each select="ext:node-set($vrtfSplit)/*">
   <processedItem>
    <xsl:value-of select="10 * ."/>
   </processedItem>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="text()" name="split">
  <xsl:param name="pText" select="."/>
   <xsl:if test="string-length($pText) >0">
    <item>
     <xsl:value-of select=
      "substring-before(concat($pText, ','), ',')"/>
    </item>

    <xsl:call-template name="split">
     <xsl:with-param name="pText" select=
     "substring-after($pText, ',')"/>
    </xsl:call-template>
   </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied to the following XML document:

<mark>1,2,3,4,5</mark>

The wanted, correct output (each item multiplied by 10) is produced:

<processedItem>10</processedItem>
<processedItem>20</processedItem>
<processedItem>30</processedItem>
<processedItem>40</processedItem>
<processedItem>50</processedItem>

II. XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="mark">
  <xsl:for-each select="tokenize(., ',')">
   <processedItem>
    <xsl:sequence select="10*xs:integer(.)"/>
   </processedItem>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

The explaination by Dimitre Novatchev is awesome, but we can also do it in much more simpler way without using node-set() function have a look:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">


    <xsl:output omit-xml-declaration="yes" indent="yes"/>



    <xsl:variable name="delimiter">
        <xsl:text>,</xsl:text>
    </xsl:variable>



    <xsl:template match="mark">
        <xsl:variable name="dataList">
            <xsl:value-of select="."/>
        </xsl:variable>
        <xsl:call-template name="processingTemplate">
            <xsl:with-param name="datalist" select="$dataList"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="processingTemplate">
        <xsl:param name="datalist"/>


        <xsl:choose>
        <xsl:when test="contains($datalist,$delimiter)  ">
                <xsl:element name="processedItem">
                    <xsl:value-of select="substring-before($datalist,$delimiter) * 10"/>
                </xsl:element>
                <xsl:call-template name="processingTemplate">
                    <xsl:with-param name="datalist" select="substring-after($datalist,$delimiter)"/>
                </xsl:call-template>
        </xsl:when>
            <xsl:when test="string-length($datalist)=1">
                <xsl:element name="processedItem">
                    <xsl:value-of select="$datalist * 10"/>

                    </xsl:element>
            </xsl:when>
        </xsl:choose>    

    </xsl:template>
</xsl:stylesheet>

In 1.0 you need to write a recursive template - except you don't, because it's already been written. Download the str:tokenize template from http://www.exslt.org.


If you can use exslt there's a tokenize() function that will do this nicely.

node-set str:tokenize(string, string?)

See http://www.exslt.org/str/functions/tokenize/