Re: text retrieval problem

Subject: Re: text retrieval problem
From: "Steve Muench" <smuench@xxxxxxxxxxxxx>
Date: Wed, 9 Aug 2000 20:44:21 -0700
| How do I get the last number in a string like:
| "[sdsdf] 234 sdf 50"

Matt,

There may be some tricker way to do this all in a single
function, but here's an example of the recursive
call-template technique.

Assume your input document is:

<List>
   <Part>[sdsdf] 234 sdf 50</Part>
   <Part>1123</Part>
   <Part>ABC 123</Part>
</List>

To produce a result like:

<result>
   <LastNum>50</LastNum>
   <LastNum>1123</LastNum>
   <LastNum>123</LastNum>
</result>

Your stylesheet would be:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output indent="yes"/>
  <xsl:template match="/List">
   <result>
     <xsl:for-each select="Part">
       <LastNum>
         <xsl:call-template name="last-number">
           <xsl:with-param name="str" select="."/>
         </xsl:call-template>
       </LastNum>
     </xsl:for-each>
   </result>
  </xsl:template>

  <!-- Example using recursive named template -->
  <xsl:template name="last-number">
    <xsl:param name="str"/>
    <!-- Set a variable to what's after the first space in $str -->
    <xsl:variable name="left" select="substring-after($str,' ')"/>
    <xsl:choose>
      <!--
       | If nothing is after a space, then there was no space 
       | So the current $str is the last "token"
       +-->
      <xsl:when test="$left=''">
        <!-- Construct the number value of the current $str -->
        <xsl:value-of select="number($str)"/>
      </xsl:when>
      <xsl:otherwise>
        <!--
         | If we found something after a space, then invoke 
         | Ourselves recursively with what's $left to see if
         | there aren't other spaces left
         +-->
        <xsl:call-template name="last-number">
          <xsl:with-param name="str" select="$left"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

______________________________________________________________
Steve Muench, Lead XML Evangelist & Consulting Product Manager
BC4J & XSQL Servlet Development Teams, Oracle Rep to XSL WG
Author "Building Oracle XML Applications", O'Reilly
http://www.oreilly.com/catalog/orxmlapp/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread