Re: Escaping for use in URLs

Subject: Re: Escaping for use in URLs
From: "Steve Muench" <smuench@xxxxxxxxxxxxx>
Date: Fri, 5 May 2000 13:24:30 -0700
My post bounced yesterday... Here's a resend...

| and I miss spoke the last time . . . it basically needs to make the value
| URL-encoded . . . meaning
| 
| space  =    %20
| /         =     %2F
| ?        =     %3F
| etc . . . .

You could use a "replace-this-with-that" kind of
named template along the lines of this below. It
takes the string "test / ?" and produces:

<a href="test%20%2F%20%3F"/>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

  <!-- Example illustrating how to use QuoteURL -->
  <xsl:template match="/">
    <a>
      <xsl:attribute name="href">
        <xsl:call-template name="QuoteURL">
          <xsl:with-param name="text">test / ?</xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
    </a>
  </xsl:template>

  <xsl:template name="QuoteURL">
    <xsl:param name="text"/>
    <xsl:call-template name="ReplaceThisWithThat">
      <xsl:with-param name="text">
        <xsl:call-template name="ReplaceThisWithThat">
          <xsl:with-param name="text">
            <xsl:call-template name="ReplaceThisWithThat">
              <xsl:with-param name="text">
                <xsl:value-of select="$text"/>
              </xsl:with-param>
              <xsl:with-param name="this" select="'/'"/>
              <xsl:with-param name="that" select="'%2F'"/>
            </xsl:call-template>
          </xsl:with-param>
          <xsl:with-param name="this" select="'?'"/>
          <xsl:with-param name="that" select="'%3F'"/>
        </xsl:call-template>
      </xsl:with-param>
      <xsl:with-param name="this" select="' '"/>
      <xsl:with-param name="that" select="'%20'"/>
    </xsl:call-template>
  </xsl:template>

  <!-- Replace occurrence of this with that  -->
  <xsl:template name="ReplaceThisWithThat">
    <xsl:param name="text"/>
    <xsl:param name="this"/>
    <xsl:param name="that"/>
    <xsl:choose>
    <xsl:when test="contains($text,$this)">
        <xsl:value-of select="substring-before($text,$this)"/>
        <xsl:value-of select="$that"/>
        <xsl:call-template name="ReplaceThisWithThat">
          <xsl:with-param name="text" select="substring-after($text,$this)"/>
          <xsl:with-param name="this" select="$this"/>
          <xsl:with-param name="that" select="$that"/>
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

______________________________________________________________
Steve Muench, Lead XML Evangelist & Consulting Product Manager
Business Components for Java & XSQL Servlet Development Teams
Oracle Rep to the W3C XSL Working Group


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


Current Thread