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

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"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- 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:param>
  <xsl:choose>
    <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:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$procstring"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

-- 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"/>
  <b>
    <xsl:value-of select="$find"/>
  </b>
</xsl:template>

(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


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 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: http://www.mulberrytech.com/xsl/xsl-list




Current Thread
Keywords
xml