[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: "James A. Robinson" <jim.robinson@xxxxxxxxxxxx>
Date: Thu, 20 Oct 2005 09:45:00 -0700
xslt string manipulation
> I have a string of the form babc.def.ghib (java namespace ) where
> babc.defb is the package name and bghib is the class name.  I need to
> extract these two from the complete string : babc.def.ghibB  -------b
> babc.defbB  + bghib
> 
> In java this would take about 1-2 lines of code, but in XSLT I cannot
> figure out a way to do it without writing tons of code.  Why is the
> support for string manipulation and regular expressions non-existent
> in XSLT, when XML is all about text ( more than java etc..  )??
> 
> Ibm constantly frustrated by trying to write little templates to do
> these simple things like splitting a string etc.

In XSLT 1.0 I do think string handling is a bit awkward, but I
wouldn't have thought it was considered a HUGE amount of code:

  <xsl:template name="package">
    <xsl:param name="class" />   <!-- name of class (e.g., org.highwire.bar.Baz -->
    <xsl:param name="package" /> <!-- private use -->
    <xsl:choose>
      <xsl:when test="contains($class , '.')">
        <xsl:call-template name="package">
          <xsl:with-param name="class" select="substring-after($class, '.')" />
          <xsl:with-param name="package" select="concat($package, '.', substring-before($class, '.'))" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$package" />
        <xsl:text>, </xsl:text>
        <xsl:value-of select="$class" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

You would, of course, modify the xsl:otherwise to return something in the
form you wanted. I just used a string In XSLT 2.0 it is easier, since we
have regex:

  <xsl:template name="package">
    <xsl:param name="class" /> <!-- name of class (e.g., org.highwire.bar.Baz -->
    <xsl:analyze-string select="$class" regex="(.+)\.([^\.]+)$">
      <xsl:matching-substring>
        <xsl:value-of select="regex-group(1)"/>
        <xsl:text>, </xsl:text>
        <xsl:value-of select="regex-group(2)"/>
      </xsl:matching-substring>
    </xsl:analyze-string>
  </xsl:template>
  
Jim

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       jim.robinson@xxxxxxxxxxxx
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)

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.