Skip to content

Instantly share code, notes, and snippets.

@emiliano-poggi
Created May 10, 2011 11:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emiliano-poggi/964314 to your computer and use it in GitHub Desktop.
Save emiliano-poggi/964314 to your computer and use it in GitHub Desktop.
XSLT: Split list in tokens
<root>
<varlist>a.a.a b.b.b c.c.c</varlist>
</root>
<nodelist>
<id>a.a.a</id>
<id>b.b.b</id>
<id>c.c.c</id>
</nodelist>
<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="/root">
<xsl:variable name="varList" select="varlist" />
<xsl:variable name="nodelist">
<xsl:call-template name="output-tokens">
<xsl:with-param name="list"><xsl:value-of select="$varList"/></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<nodelist>
<xsl:copy-of select="$nodelist"/>
</nodelist>
</xsl:template>
<xsl:template name="output-tokens">
<xsl:param name="list" />
<xsl:variable name="newlist" select="concat(normalize-space($list), ' ')" />
<xsl:variable name="first" select="substring-before($newlist, ' ')" />
<xsl:variable name="remaining" select="substring-after($newlist, ' ')" />
<id>
<xsl:value-of select="$first" />
</id>
<xsl:if test="$remaining">
<xsl:call-template name="output-tokens">
<xsl:with-param name="list" select="$remaining" />
</xsl:call-template>
</xsl:if>
</xsl:template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment