Skip to content

Instantly share code, notes, and snippets.

@ijy
Created September 18, 2013 20:16
Show Gist options
  • Save ijy/6614997 to your computer and use it in GitHub Desktop.
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/
<!-- 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" />
@ijy
Copy link
Author

ijy commented Sep 18, 2013

In this case math:random()*10 ranges from 0 to 10, with 10 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 by mod 10 and + 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment