Created
December 9, 2018 15:53
-
-
Save jlafitte/a760c24fe0bde33b393287140d743a21 to your computer and use it in GitHub Desktop.
XSLT URL Encoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> | |
<xsl:output method="xml" indent="yes" encoding="iso-8859-1"/> | |
<!-- ISO-8859-1 based URL-encoding demo | |
Written by Mike J. Brown, [email protected]. | |
Updated 2015-10-24 (to update the license). | |
License: CC0 <https://creativecommons.org/publicdomain/zero/1.0/deed.en> | |
Also see http://skew.org/xml/misc/URI-i18n/ for a discussion of | |
non-ASCII characters in URIs. | |
--> | |
<!-- The string to URL-encode. | |
Note: By "iso-string" we mean a Unicode string where all | |
the characters happen to fall in the ASCII and ISO-8859-1 | |
ranges (32-126 and 160-255) --> | |
<xsl:param name="iso-string" select="'¡Hola, César!'"/> | |
<!-- Characters we'll support. | |
We could add control chars 0-31 and 127-159, but we won't. --> | |
<xsl:variable name="ascii"> !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable> | |
<xsl:variable name="latin1"> ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ</xsl:variable> | |
<!-- Characters that usually don't need to be escaped --> | |
<xsl:variable name="safe">!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~</xsl:variable> | |
<xsl:variable name="hex" >0123456789ABCDEF</xsl:variable> | |
<xsl:template match="/"> | |
<result> | |
<string> | |
<xsl:value-of select="$iso-string"/> | |
</string> | |
<hex> | |
<xsl:call-template name="url-encode"> | |
<xsl:with-param name="str" select="$iso-string"/> | |
</xsl:call-template> | |
</hex> | |
</result> | |
</xsl:template> | |
<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:message terminate="no">Warning: string contains a character that is out of range! Substituting "?".</xsl:message> | |
<xsl:text>63</xsl:text> | |
</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('%',$hex-digit1,$hex-digit2)"/> | |
</xsl:otherwise> | |
</xsl:choose> | |
<xsl:if test="string-length($str) > 1"> | |
<xsl:call-template name="url-encode"> | |
<xsl:with-param name="str" select="substring($str,2)"/> | |
</xsl:call-template> | |
</xsl:if> | |
</xsl:if> | |
</xsl:template> | |
</xsl:stylesheet> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment