Re: [xsl] RE: String manipulation in XSLT

Subject: Re: [xsl] RE: String manipulation in XSLT
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Mon, 24 Oct 2005 22:06:34 +0530
Please try this stylesheet

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                       xmlns:common="http://exslt.org/common";
                       version="1.0">

<xsl:output method="text" />

<xsl:variable name="str" select="'abc.def.ghi'" />

<xsl:template match="/">
  <xsl:variable name="rtf">
    <string>
      <xsl:call-template name="reform_string">
        <xsl:with-param name="str" select="$str" />
        <xsl:with-param name="delim" select="'.'" />
      </xsl:call-template>
    </string>
  </xsl:variable>
  <xsl:variable name="package">
    <xsl:for-each select="common:node-set($rtf)/string/part">
      <xsl:if test="position() != last()">
        <xsl:value-of select="." />
      </xsl:if>
      <xsl:if test="position() &lt; (last() - 1)">
        .
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  Package name - <xsl:value-of select="translate($package,'&#xa; ','')" />
  Class name - <xsl:value-of
select="common:node-set($rtf)/string/part[last()]" />
</xsl:template>

<xsl:template name="reform_string">
  <xsl:param name="str" />
  <xsl:param name="delim" />

  <xsl:choose>
    <xsl:when test="contains($str,'.')">
      <part><xsl:value-of select="substring-before($str,'.')" /></part>
      <xsl:call-template name="reform_string">
        <xsl:with-param name="str" select="substring-after($str,'.')" />
        <xsl:with-param name="delim" select="$delim" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <part><xsl:value-of select="$str" /></part>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

This was tested with Saxon 8.5.1 as
java net.sf.saxon.Transform file.xsl file.xsl

(The stylesheet filename is file.xsl)

Hope you can take some ideas from this..

>I'm constantly frustrated by trying to write little templates to do
these simple >things like splitting a string etc.
I do enjoy writing XSLT stylesheets! If you can, you can move to XSLT
2.0. It has lot of new constructs and functions, which reduce the
program size considerably.

Regards,
Mukul

Current Thread