xsl-list
[Top] [All Lists]

Re: RE: String manipulation in XSLT

2005-10-20 09:45:00

I have a string of the form â??abc.def.ghiâ?? (java namespace ) where
â??abc.defâ?? is the package name and â??ghiâ?? is the class name.  I need to
extract these two from the complete string : â??abc.def.ghiâ??  -------â??
â??abc.defâ??  + â??ghiâ??

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..  )??

Iâ??m 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(_dot_)robinson(_at_)stanford(_dot_)edu
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--