Created
September 18, 2013 20:16
-
-
Save ijy/6614997 to your computer and use it in GitHub Desktop.
Generate a random number between 1- 10 in XSLT. @href: http://www.getsymphony.com/discuss/thread/81429/
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
<!-- Add the 'math' namespace to your XML stylesheet (so we can use 'math:random') --> | |
<xsl:stylesheet version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns:math="http://exslt.org/math" | |
extension-element-prefixes="math"> | |
<!-- Pick a random number between 1 - 10 and set as a variable --> | |
<xsl:value-of select="(floor(math:random()*10) mod 10) + 1" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this case
math:random()*10
ranges from0 to 10
, with10
being almost unlikely. The trick is to strip it out of the allowed values and then shift the result set({0, 1, 2, ...})
by one position to the right. This is done respectively bymod 10
and+ 1
.