Re: [xsl] Is recursion in XSL really this difficult?

Subject: Re: [xsl] Is recursion in XSL really this difficult?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 17 Jul 2001 17:15:50 -0400
Hello Anonymous:

Ordinarily, I'm reluctant to answer posts from people who don't sign their name, since I like to have at least *some* clue of who they are.

But since I tackled a problem somewhat like yours back when in a project I distributed, I thought I'd post it.

Note that this recursive string-finding bit doesn't do a case-insensitive match, or make allowances for whitespace (which, judging from a cursory glance at your code, you apparently want). But it might give you a place to start.

Import this stylesheet into yours:

<xsl:stylesheet version="1.0"

<!-- The 'segmentfunc' named template returns, for a given node,
     a text string with all occurrences of a given substring
     isolated by markup. The substring to be isolated must be
     passed to the template as the 'find' parameter: its markup
     is provided by another named template, 'displayfound' (to
     be provided in the main stylesheet).

Wendell Piez, Mulberry Technologies, December 1999. -->

<xsl:template name="segmentfunc">
  <xsl:param name="procstring">
    <!-- the string to be processed. Trimmed recursively until
         all occurrences have been isolated and passed to
         'display'.                                           -->
    <xsl:value-of select="."/>
    <xsl:when test="$find and contains($procstring, $find)">
      <xsl:value-of select="substring-before($procstring, $find)"/>
      <xsl:call-template name="displayfound"/>
      <xsl:call-template name="segmentfunc">
        <xsl:with-param name="procstring"
                      select="substring-after($procstring, $find)"/>
      <xsl:value-of select="$procstring"/>


-- and then, place a template such as this one in your main stylesheet (to provide your found string with markup in the output) --

<xsl:template name="displayfound">
  <!-- Providing markup for the found string. -->
  <xsl:param name="displaystring"/>
    <xsl:value-of select="$find"/>

(This one wraps your found string in a <b> element, assuming that string is identified in a global variable or parameter $find; do what you will.)

I hope that helps.


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

XSL-List info and archive:

Current Thread