String tokenize using XSLT

Hi,

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.

Why 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”?>
<product>
 <code>
  <keys>prdName=SOME_PRD_NAME;catg=SOME_CATG_NAME;price=1234;prdcd=234;</keys>
   </code>

And the expected output:

<product>
<productName>SOME_PRD_NAME</productName>
<categoryName>SOME_CATG_NAME</categoryName>
<price>1234</price>
<productCode>224</productCode>

*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=”http://www.w3.org/1999/XSL/Transform“>
<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”/>
  xsl:with-param>
 xsl:call-template>
      xsl:for-each>
 
xsl:template>

<!– 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:choose>
 <xsl:when test=”$delimiter and contains($string, $delimiter)”>
 <xsl:variable name=”temp”>
  <xsl:value-of select=”substring-before($string,$delimiter)” />
 xsl:variable>
 <xsl:call-template name=”createTag”>
           <xsl:with-param name=”keyVal” select=”$temp” />
 xsl:call-template>
 <xsl:call-template name=”extractKey”>
           <xsl:with-param name=”string” select=”substring-after($string,$delimiter)” />
 xsl:call-template>
    xsl:when>
xsl:choose>

xsl:template>

<!– 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:choose>
  <xsl:when test=”$delimiter1 and contains($keyVal, $delimiter1)”>
  <xsl:variable name=”dbKeyName”>
   <xsl:value-of select=”substring-before($keyVal,$delimiter1)” />
  xsl:variable>
    <xsl:element name=”{$dbKeyName}”>
   <xsl:value-of select=”substring-after($keyVal,$delimiter1)” />
   xsl:element>
     xsl:when>
 xsl:choose>
xsl:template>
xsl:stylesheet>

 </product>
 
</catalog>

Output

<?xml version=”1.0″ encoding=”UTF-16″?>
<prdName>SOME_PRD_NAME</prdName>
<catg>SOME_CATG_NAME</catg>
<price>1234</price>
<prdcd>234</prdcd>

Hope this helps.


R Vashi

3 thoughts on “String tokenize using XSLT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s