[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] RE: String manipulation in XSLT


Subject: Re: [xsl] RE: String manipulation in XSLT
From: "James A. Robinson" <jim.robinson@xxxxxxxxxxxx>
Date: Thu, 20 Oct 2005 09:45:00 -0700

> 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
Keywords