Sunday, July 21, 2013

How to use String tokenize function in XSLT


Here is an example to use String function: tokenize()
Say the csv has element Role seperated by " ; " and we need to translate using tokenize function:

Roles: Supplier;Vendor;Investor

<Roles>
             <Role>Supplier</Role>
             <Role>Vendor</Role>
             <Role>Investor</Role>
</Roles>

XSL Code:

  <Roles>
                <xsl:call-template name="tokenize">
                  <xsl:with-param name="string"
                                  select="/ns0:Role"/>
                </xsl:call-template>
              </Roles>

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

  </xsl:template>

No comments:

Post a Comment