1

How to create a Random string longer than 25 characters consisting of of digits and letters with XSLT?

Example: Khb34KXQ23ib34KDNBBE342nQE

My XSLT is like this:

<xsl:function name="kh:shortRandom">
        <xsl:sequence select="generate-id()"/>
    </xsl:function>
    
  
   <xsl:template match="/">
        <test>
            <randomId><xsl:value-of select="concat(kh:shortRandom(), kh:shortRandom(), kh:shortRandom(), kh:shortRandom())"/></randomId>
        </test>
    </xsl:template>

But the answer is always the same..(e1d1).. Because i call the function four times.. the answer is also four time. (e1d1e1d1e1d1e1d1)

I want to have a different character every time. A little bit like password generator but just with letters and numbers.

Tnx :)

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
Mozamel
  • 67
  • 5
  • I found [this thread](https://stackoverflow.com/questions/30195871/generate-random-and-unique-string-xslt) for you. Hope it helps. – andexte May 06 '22 at 09:00
  • select="generate-id()" delivers the same character every time. I would like to have defferent character every time. Like a password generator but just with numbers and letters. – Mozamel May 06 '22 at 09:08
  • There is no random function available in XSLT 1.0 to 2.0. Which XSLT processor are you using? – michael.hor257k May 06 '22 at 10:50
  • @Mozamel , When using XSLT 2.0 or even 1.0, one can easily create the wanted function with the help of FXSL, as shown in my answer. For more information, please see: http://fxsl.sourceforge.net/articles/Random/Casting%20the%20Dice%20with%20FXSL-htm.htm – Dimitre Novatchev May 31 '22 at 23:58

2 Answers2

0

In XSLT 3.0 (XPath 3.1) one can use the random-number-generator() function.

For XSLT 2.0 I would recommend using the random number functions of FXSL - see for example: "Casting the Dice with FXSL: Random Number Generation Functions in XSLT"

Using this, here is an implementation of the wanted random-string generation function:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="my">
 <xsl:import href="C:/CVS-DDN/fxsl-xslt2/f/random.xsl"/>
 <xsl:output method="text"/>
 
 <xsl:variable name="vSeed" select=
   "xs:integer(seconds-from-time(current-time()))
   * xs:integer(minutes-from-time(current-time()))
   * xs:integer(hours-from-time(current-time()))"/>

  <xsl:template match="/">
     <xsl:value-of select="my:randomStrings(25, 10, $vSeed)" separator="&#xA;"/>
  </xsl:template>

  <xsl:function name="my:randomStrings">
    <xsl:param name="pRandomLength" as="xs:integer"/>
    <xsl:param name="pNumResults" as="xs:integer" />
    <xsl:param name="pSeed" as="xs:integer"/>
    
    <xsl:variable name="vAlphaNum" select="'abcdefghijklmnopqrstuvwxyz0123456789'"/>

    <xsl:variable name="vNums">
       <xsl:call-template name="randomSequence">
          <xsl:with-param name="pSeed" select="$pSeed"/>
          <xsl:with-param name="pStart" select="1"/>
          <xsl:with-param name="pEnd" select="36"/>
          <xsl:with-param name="pLength" select="$pRandomLength*$pNumResults"/>
       </xsl:call-template>
    </xsl:variable>
    
    <xsl:sequence select=
     "for $vK in 1 to $pNumResults
        return
          string-join(for $i in
                            $vNums/*[position() gt ($vK -1)*$pRandomLength 
                                   and position() le $vK*$pRandomLength]
                                         /xs:integer(.)
                        return substring($vAlphaNum, $i, 1),
                      '')
          "/>
  </xsl:function>
 </xsl:stylesheet>

The function my:randomStrings(pRandomLength, pNumResults, pSeed) implemented above produces a sequence of random strings and has these three arguments:

  1. pRandomLength - the wanted length of each generated random string
  2. pNumResults - the wanted number of random strings to be generated
  3. pSeed - a seed for the random generator. Calling the function with different seeds will produce different results.

The code above calls the function to produce 10 random strings each with length 25. The seed is calculated from the current time and thus the result will be different each time the transformation is performed.

Here is one result:

azdkex5yi5rm3suewa7bxazpc
qi2qsg7qvl7en4cx2c5s9vfrp
l8t0lv659uba500t6e7fea518
7bt80g6bpjtjltna7ru6e3t15
t90s62fvnex5yqcq2osv97n5z
hibzw8g95wv15x2s2wv8cobem
dqiubm165tp1pci34hparuqs7
5d0chkl85liaowx3v88isk4oo
6iw5iktzaqa7jnf4g9lakqdhk
insg7iggsc22fqd1jkhbrxo53

And here is another:

bstudsgn85xq7dncy9fubu8we
g9hkl0qf493u0x7xmaz0hunqd
9lyclhrp19iz33v0hdmt7txoh
b45t1t1xfves5fjn3syzilhjq
p5bh89iojemh7adb41suew20d
goznie54278vfb4968zx3n9o8
lmouaz8j7i033mtjx1t6ymbjn
jxgqajz7g9db0g6j4o8l6ukgw
2ge6nhv69emcqanc6f63yeoro
yws75ttmbnsbyxvwwch86wbe2

Note:

As noted, you need to have downloaded the FXSL library and you will need to set the href attribute of the <xsl:import> declaration above, to point to the exact location of the file system, where the imported stylesheet file resides.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
-1
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const charactersLength = characters.length;

const generate25letterrandomstring = () => {
    let randomString = '';
    for (let i = 0; i < 25; i++) {
        randomString += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    console.log(randomString);
    return randomString;
}

generate25letterrandomstring()
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ruben Verster
  • 301
  • 1
  • 8