Random numbering in XSLT (Java)

There are no translations available.

Sometimes you need to generate unique identifiers for generated XML elements. This article describes how to obtain random ids when executing transformation in Java.

Random number generator

<xsl:variable name="random" select="java:java.util.Random.new()"/>
{geshi}<xsl:template name="random_id"><xsl:value-of
select="format-number(round(9999 * java:nextDouble($random)), '0000')" /></xsl:template>
{/geshi}
<xsl:template match="NaryOperator[@id='72']"     ><math:apply><xsl:attribute 
name="id"><xsl:call-template name="random_id" /></xsl:attribute><math:times/><xsl:apply-templates
/></math:apply></xsl:template>
<xsl:stylesheet  
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:java="http://xml.apache.org/xslt/java"
>
Last Updated on Wednesday, 08 April 2009 12:14