Skip to content

Instantly share code, notes, and snippets.

@designermonkey
Created January 16, 2013 21:41
Show Gist options
  • Save designermonkey/4551196 to your computer and use it in GitHub Desktop.
Save designermonkey/4551196 to your computer and use it in GitHub Desktop.
<data>
<thumbs>
<item>120</item>
<item>240</item>
<item>360</item>
</thumbs>
</data>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:math="http://exslt.org/math"
extension-element-prefixes="exsl math">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="widths">
<item>120</item>
<item>240</item>
<item>360</item>
</xsl:variable>
<xsl:template match="/">
<xsl:call-template name="find-random-node"/>
</xsl:template>
<xsl:template name="find-random-node">
<xsl:variable name="nodes" select="exsl:node-set($widths)"/>
<xsl:variable name="node-count" select="count($nodes/item)"/>
<xsl:variable name="random-node" select="ceiling(math:random() * $node-count)"/>
<count><xsl:value-of select="$node-count"/></count>
<random><xsl:value-of select="$random-node"/></random>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment