Author:
wjnielsen
Version:
1.0
Release Date:
27 Feb 2014
Category:
String Operations

Description

Generates an alpha numeric hash of a specified length using xslt 1. 0 and exlt math library.

The first character of the hash is always a letter.

XSLT

View Raw
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"     xmlns:math="http://exslt.org/math"
    extension-element-prefixes="math">
 
    <xsl:template name="makeHash">
        <xsl:param name="length">8</xsl:param>
        
        <xsl:param name="count" select="$length"/>
        <xsl:param name="hash"/>
        
        <xsl:variable name="possibleFirst">xABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz</xsl:variable>
        <xsl:variable name="possible">xABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789</xsl:variable>
        <xsl:variable name="possibleFirstLength" select="string-length($possibleFirst)"/>
        <xsl:variable name="possibleLength" select="string-length($possible)"/>       
                
        <xsl:choose>
            <xsl:when test="$count = $length">
                <xsl:variable name="position" select="floor(math:random()* $possibleFirstLength)" />
                <xsl:call-template name="makeHash">
                    <xsl:with-param name="length" select="$length"/>
                    <xsl:with-param name="count" select="$count - 1"/>
                    <xsl:with-param name="hash">
                        <xsl:value-of select="$hash"/>
                        <xsl:value-of select="substring($possibleFirst, $position, 1)"/>                        
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$count &gt; 0">
                <xsl:variable name="position" select="floor(math:random()* $possibleLength)" />
                <xsl:call-template name="makeHash">
                    <xsl:with-param name="length" select="$length"/>
                    <xsl:with-param name="count" select="$count - 1"/>
                    <xsl:with-param name="hash">
                        <xsl:value-of select="$hash"/>
                        <xsl:value-of select="substring($possible, $position, 1)"/>                        
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$hash"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Discuss this XSLT Utility

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