Saturday, June 1, 2013

How to implement String Tokenizer using XSLT2.0

How to implement String Tokenizer using XSLT2.0

http://www.xml.com/lpt/a/1205

How to define a variable and assign a value using XSLT

How to define a variable and assign a value using XSLT

Define an element

<xsl:variable name="sampleString">XML,XSLT,XPath,SVG,XPointer</xsl:variable>

<xsl:variable name="empName" select ="emp:EmployeeName"/>

Access the element

<name>
   <xsl:value-of select="$empName"/>
</name>

How to use apply-template and call-template in XSLT

How to use <xsl:call-template> and <xsl:apply-template>
How to implement String tokenizer functionality using XSLT

 I will demo an example how to use call-template recursively in XSLT, that is to tokenizea given string based on specified delimiter.

<xsl:call-template> - Using this, we can invoke a template by name of the template

This is how we can define a template with the name as 'tokenize' and which accepts two parameters 

<xsl:template name="tokenize">
    <xsl:param name="separator" select="';'"/>
    <xsl:param name="string"/>
    <xsl:choose>
      <xsl:when test="contains($string,$separator)">
        <ns0:Role>
          <xsl:value-of select="substring-before($string,$separator)"/>
        </ns0:Role>
        <xsl:call-template name="tokenize">
          <xsl:with-param name="string"
                          select="substring-after($string,$separator)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <ns0:Role>
          <xsl:value-of select="$string"/>
        </ns0:Role>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

- This is how we invoke the template by passing the parameters
                <xsl:call-template name="tokenize">
                  <xsl:with-param name="string"
                                  select="oim:StringWithDelimiter"/>
                </xsl:call-template>

<xsl:apply-template> - Using this we can invoke template by matching a specific element.

This is how we can define a template to invoke using <apply-template>

<xsl:template match="emp:Employee">
   <emp:Name>
      <xsl:value-of select="emp:FirstName"/>
    </emp:Name>
  </xsl:tempate>

How to invoke a template using <apply-template>

<xsl:apply-templates select="/emp:Employee"/>