The following simple solution greatly assisted me with XSLT 2
(generates random number):
sum(string-to-codepoints(generate-id($generated//random))
Or you can use it as a function:
<xsl:function name=“your:random-int" as="xs:integer">
<xsl:variable name="generated">
<random/>
</xsl:variable>
<xsl:value-of select="sum(string-to-codepoints(generate-id($generated//random)))"/>
</xsl:function>
Here's how it works:
- The
generate-id()
function generates a string containing the generated id, such as wfx2d123d8
.
- The
string-to-codepoints()
function transforms the string obtained in the previous step into a list of nodes of ASCII codes of the string , similar to the following: (119, 102, 120, 50, 100, 49, 50, 51, 100, 56)
.
- Finally, the
sum()
function calculates the sum of all the numbers obtained in step 2, resulting in a single value
As you can observe, the random number generator mentioned is not reliable and lacks a uniform distribution. Therefore, I strongly advise against using it for important tasks. However, if you require a simple solution and are unconcerned about distribution, as was the case for me, it could potentially serve as an option.