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="
"/>
</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:
pRandomLength
- the wanted length of each generated random string
pNumResults
- the wanted number of random strings to be generated
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.