RE: [xsl] Second try: Search and replace many strings that may not be present in target

Subject: RE: [xsl] Second try: Search and replace many strings that may not be present in target
From: "Andrew Welch" <awelch@xxxxxxxxxxxxxxx>
Date: Mon, 20 May 2002 17:07:10 +0100
Ok, just for fun :) here is a stylesheet that doesnt need the javascript
to find out if its the first occurance of the name.

It does this by taking each text() node and getting the
substring-before() of all the text() nodes in the entire xml doc (no,
really :)

It then tests to see if this substring contains the name that is
currently being worked on, if it doesnt (ie this is the first occurance)
then it outputs the <span> (which I havent got around to changing to the
link...) otherwise it outputs the plain text().

If two names occur in one text() node when one has occured before and
one hasnt then of course they will both 'fail'... there is no way around

If you really want to do this properly (and much more simply), then you
will need to do two passes.

have fun :)


<xsl:stylesheet xmlns:xsl="";

<xsl:variable name="quotes-rtf">
  <xsl:for-each select="//quote">
    <quote who="{@who}"/>

<xsl:variable name="quotes" select="msxsl:node-set($quotes-rtf)"/>
<xsl:variable name="theRoot" select="/"/>
<xsl:variable name="everything" select="$theRoot//text()"/>

<xsl:template match="/">

<xsl:template name="checkIfFirst">
 <xsl:param name="theText" select="nothing"/>
 <xsl:param name="who" select="noone"/>
 <xsl:variable name="precedingText"
   <xsl:when test="contains($precedingText,$who)">
     <xsl:value-of select="$theText"/>
      <xsl:value-of select="substring-before($theText,$who)"/>
        <quoteLink><xsl:value-of select="$who"/></quoteLink>
      <xsl:value-of select="substring-after($theText,$who)"/>

<xsl:template name="checkForName">
  <xsl:param name="theText" select="notext"/>
  <xsl:param name="position" select="1"/>
  <xsl:variable name="who" select="$quotes/quote[$position]/@who"/>
    <xsl:when test="count($quotes/quote) >= $position">
    <xsl:when test="contains($theText,$who)">
      <xsl:call-template name="checkIfFirst">
        <xsl:with-param name="theText" select="$theText"/>
        <xsl:with-param name="who" select="$who"/>
      <xsl:call-template name="checkForName">
        <xsl:with-param name="theText" select="$theText"/>
        <xsl:with-param name="position" select="$position + 1"/>
  <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>

<xsl:template match="text()">
  <xsl:variable name="theText" select="."/>
  <xsl:call-template name="checkForName">
    <xsl:with-param name="theText" select="."/>

<xsl:template match="section">
  <xsl:apply-templates select="text()"/>


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (
Version: 6.0.350 / Virus Database: 196 - Release Date: 17/04/2002

 XSL-List info and archive:

Current Thread