- How to implement XSLT tokenize function?
- <xsl:stylesheet
- version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:func="http://exslt.org/functions"
- xmlns:exsl="http://exslt.org/common"
- xmlns:my="http://mydomain.com/">
- <func:function name="my:tokenize">
- <xsl:param name="string"/>
- <xsl:param name="separator" select="'|'"/>
- <xsl:variable name="item" select="substring-before(concat($string,$separator),$separator)"/>
- <xsl:variable name="remainder" select="substring-after($string,$separator)"/>
- <xsl:variable name="tokens">
- <token><xsl:value-of select="$item"/></token>
- <xsl:if test="$remainder!=''">
- <xsl:copy-of select="my:tokenize($remainder,$separator)"/>
- </xsl:if>
- </xsl:variable>
- <func:result select="exsl:node-set($tokens)"/>
- </func:function>
- <xsl:template match="/">
- <xsl:copy-of select="my:tokenize('a|b|c')"/>
- </xsl:template>
- </xsl:stylesheet>
- <token>a</token><token>b</token><token>c</token>
- abc
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:str="http://exslt.org/strings"
- extension-element-prefixes="str"
- …
- <select name="months">
- <xsl:for-each select="str:tokenize('1,2,3,4,5,6,7,8,9,10,11,12', ',')">
- <xsl:element name="option">
- <xsl:attribute name="value">
- <xsl:value-of select="."/>
- </xsl:attribute>
- <xsl:value-of select="."/>
- </xsl:element>
- </xsl:for-each>
- </select>
- <xsl:template name="tokenize">
- <xsl:param name="string"/>
- <xsl:param name="separator" select="'|'"/>
- <xsl:choose>
- <xsl:when test="contains($string,$separator)">
- <token>
- <xsl:value-of select="substring-before($string,$separator)"/>
- </token>
- <xsl:call-template name="tokenize">
- <xsl:with-param name="string" select="substring-after($string,$separator)"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <token><xsl:value-of select="$string"/></token>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
- <xsl:call-template name="tokenize">
- <xsl:with-param name="string" select="'a|b|c'"/>
- </xsl:call-template>
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:ext="http://exslt.org/common">
- <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">
- <xsl:value-of select="concat(position(), ' ', ., ' ')"/>
- </xsl:template>
- </xsl:stylesheet>
- <t>out, of
- luck</t>
- 1 out
- 2 of
- 3 luck