Re: [xsl] replacing a string value

Subject: Re: [xsl] replacing a string value
From: Peter Davis <pdavis@xxxxxxxxx>
Date: Thu, 29 Nov 2001 14:06:41 -0800
Am I right in assuming that the '?' can occur at any place in the input?:

<foo> hello ? world ? foo </foo>
would translate to:
<foo> hello <img/> world <img/> foo </foo>

This stylesheet will do what you want:

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl=""; 
  <xsl:template match="text()" name="img-replace">
    <xsl:param name="text" select="string()"/>
      <xsl:when test="not(contains($text, '?'))">
        <xsl:value-of select="$text"/>
        <xsl:value-of select="substring-before($text, '?')"/>
        <xsl:call-template name="img-replace">
          <xsl:with-param name="text" select="substring-after($text, '?')"/>
  <xsl:template match="@* | node()[not(self::text())]">
      <xsl:apply-templates select="@* | node()"/>

The second template is a modified version of the identity template, taken 
from the XSLT spec.  You can replace it with your normal processing if you 
wish.  I tested the template with the latest Xalan from their CVS, but it 
should work with just about any processor since it is 100% standard.

On Thursday 29 November 2001 01:48 pm, rick schochler wrote:
> I have a requirement to replace certain string characters (if encountered)
> with a graphic. For example, if the string value of my element foo is a
> question mark (i.e, <foo>?</foo>), I need to replace the ? with a graphic.
> Any ideas?

Furthermore, I believe bacon prevents hair loss.
Peter Davis

 XSL-List info and archive:

Current Thread