Search

Hi, I need to encode a url in XSLT to use in a “tweet this” link, is there any simple way to do this?

This stackoverflow post suggests that it might be possible to embed javascript in xslt but it seems that might be only with .NET or Sharepoint or something.

There’s also this stylesheet but it doesn’t seem to work for me.

Thanks

Can you post an example (as code) of a URL you would like to achieve?

On a side note, JavaScript doesn’t work well in XSLT, the && operator doesn’t play well…

I found this somewhere on the web; It worked for me;

<xsl:template name="url-encode">
<xsl:param name="str"/>   
<xsl:if test="$str">
  <xsl:variable name="first-char" select="substring($str,1,1)"/>
  <xsl:choose>
    <xsl:when test="contains($safe,$first-char)">
      <xsl:value-of select="$first-char"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="codepoint">
        <xsl:choose>
          <xsl:when test="contains($ascii,$first-char)">
            <xsl:value-of select="string-length(substring-before($ascii,$first-char)) + 32"/>
          </xsl:when>
          <xsl:when test="contains($latin1,$first-char)">
            <xsl:value-of select="string-length(substring-before($latin1,$first-char)) + 160"/>
          </xsl:when>
          <xsl:otherwise>
        </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
    <xsl:variable name="hex-digit1" select="substring($hex,floor($codepoint div 16) + 1,1)"/>
    <xsl:variable name="hex-digit2" select="substring($hex,$codepoint mod 16 + 1,1)"/>
    <xsl:value-of select="concat('%20',$hex-digit1,$hex-digit2)"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="string-length($str) &gt; 1">
    <xsl:call-template name="url-encode">
      <xsl:with-param name="str" select="substring($str,2)"/>
    </xsl:call-template>
  </xsl:if>
</xsl:if>

Just for the sake of thoroughness: Looks like you’ve picked Jon Miles’ solution. Bot his encoder and decoder look nicely done.

His credits say

No license; use freely, but credit me if reproducing in print.

so could anyone please take both templates and put them into a utility?

I added the Template as a utility named Url-encode, with some small adaptations. Hope this one does the job 4 u!

It turns out I had the same solution Cremol posted originally:

There’s also this stylesheet http://skew.org/xml/stylesheets/url-encode/url-encode.xsl but it doesn’t seem to work for me.it doesn’t seem to work for me.

And it had worked, just in chrome link location preview resolved the encoded url back into the decoded url so it appeared nothing had happened. I must remember to militantly check the source.

Thanks, I may not have realised I’m an idiot without your help

Cremol: I’d suggest you took away the unnecessary parts like the <xsl:apply-templates /> etc. and merged the encoder and the decoder in one utility. Kind of like this. :-)

Thanks, I updated the utility with your code… looks much better now! I’m still at the level of adapting and manipulating xslt, so i’m not 100% on top of things… but getting there with the help of this wonderfull community! :-)

Cool, congratulations on your first utility! :-)

Well, it’s not really mine… But a cool addition to the library! :p

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details