String tokenize using XSLT


First of all this is my first post on XML/XSLT. In this post I will cover how to perform the string manipulation to generate the data for different purposes using core XSLT Function. Before starting the problem lets cover some small introduction on XSLT.


Extensible Stylesheet Language Transformations (XSLT) provides a way to automatically transform XML data from one format to another. The target form is usually another XML document, but it doesn’t have to be; you can transform XML data into virtually anything simply by creating a XSLT stylesheet and processing the data. If you want to make changes to the output, you simply make changes to the stylesheet and process the XML again. This has the additional advantage of giving power to nonprogrammers, such as designers, who can edit the stylesheet and affect the results.

Hope the basic intro has given some idea about the features of XSLT and now we will see implementing a very small piece of it for String manipulation.

Few days back I got scenario where set of values were coming from external source into some CSV type string and we have to parse them in bunch of few XML tags and feed to the other end in the form ox XML.

Though XSLT provides a function “fn:tokenize” but instead of using that I will share other approach of using “substring-before”,”substring-after” XSLT Function.

Let assume you have a below input XML.

xml-stylesheet type=”text/xsl” href=”example.xslt”?>

And the expected output:


*NOTE The above xml and output is imaginary.

The XLST Code:

xml version=”1.0″ encoding=”ISO-8859-1″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”“>
<xsl:output method=”xml” version=”1.0″
encoding=”iso-8859-1″ indent=”yes”/>

<xsl:template match=”/”>
        <xsl:for-each select=”product/code”>
 <xsl:call-template name=”extractKey”>
  <xsl:with-param name=”string”>
   <xsl:value-of select=”keys”/>

<!– This template is used to first of extract the xml tag and its value –>
<xsl:template name=”extractKey”>
 <xsl:param name=”string” />
  <xsl:param name=”delimiter” select=”‘;'” />
 <xsl:when test=”$delimiter and contains($string, $delimiter)”>
 <xsl:variable name=”temp”>
  <xsl:value-of select=”substring-before($string,$delimiter)” />
 <xsl:call-template name=”createTag”>
           <xsl:with-param name=”keyVal” select=”$temp” />
 <xsl:call-template name=”extractKey”>
           <xsl:with-param name=”string” select=”substring-after($string,$delimiter)” />


<!– This template is used to create the xml tag and its value –>
<xsl:template name=”createTag”>
 <xsl:param name=”keyVal” />
 <xsl:param name=”delimiter1″ select=”‘='” />
  <xsl:when test=”$delimiter1 and contains($keyVal, $delimiter1)”>
  <xsl:variable name=”dbKeyName”>
   <xsl:value-of select=”substring-before($keyVal,$delimiter1)” />
    <xsl:element name=”{$dbKeyName}”>
   <xsl:value-of select=”substring-after($keyVal,$delimiter1)” />



<?xml version=”1.0″ encoding=”UTF-16″?>

Hope this helps.

R Vashi

3 thoughts on “String tokenize using XSLT

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.