[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: RE: String manipulation in XSLT

Subject: Re: RE: String manipulation in XSLT
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Mon, 24 Oct 2005 22:06:34 +0530
xslt 2.0 string manipulation
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.