xsl-list
[Top] [All Lists]

Re: xslt replace special characters

2002-11-08 12:51:21
At 12:40 PM 11/8/2002, you wrote:
How do you replace characters in xslt.

i have a url string that may contain '?' , '&', and '='
i want to convert

? to %3f
= to %3d
& to %26

is there a good way to do this? i try using translate function but it doesn't work that well. i've also tried writing a recursive template that checks for all occurence of these characters and it doesnt seem to work.
any idea?

  <xsl:template name="replace">
    <xsl:param name="string" select="''"/>
    <xsl:param name="pattern" select="''"/>
    <xsl:param name="replacement" select="''"/>
    <xsl:choose>
<xsl:when test="$pattern != '' and $string != '' and contains($string, $pattern)">
        <xsl:value-of select="substring-before($string, $pattern)"/>
        <!--
             Use "xsl:copy-of" instead of "xsl:value-of" so that users
             may substitute nodes as well as strings for $replacement.
        -->
        <xsl:copy-of select="$replacement"/>
        <xsl:call-template name="replace">
<xsl:with-param name="string" select="substring-after($string, $pattern)"/>
          <xsl:with-param name="pattern" select="$pattern"/>
          <xsl:with-param name="replacement" select="$replacement"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$string"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


Greg Faron
Integre Technical Publishing Co.



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