Subject: Re: [xsl] How to access to a node content randomly in xsl:for-each loop From: Massimo Santini - mailing list address <mm14ss@xxxxxxxx> Date: Mon, 18 Feb 2002 12:46:42 +0100 (CET) |
> > > I m trying to access the content of a node in random order each time > > > when I run the XSL script, is it possible? > > > > XPath doesn't have a random() function, so it isn't possible without > > using an extension function. > > Or, look at > http://www.biglist.com/lists/xsl-list/archives/200202/msg00003.html I had the same problem some times ago. The link above is one of the solutions suggested to me on this list. Actually, I'm not using that solution now (after some experiments) because it provides to little randomization ( $seed div position() is not enough since div is a monotone funcion and position() tends to be very non-random :-)... My current solution is a snippet of Java: import java.util.Random; public class exam { public static String hx( String str, int seed ) { if ( seed == 0 ) return "0"; else { Random r = new Random( str.hashCode() ); for ( int i = 0; i < seed; i++ ) r.nextInt(); return "" + r.nextInt(); } } } where I initialize the random generator with the hascode of the text in the node and than take the seed-th iteration of the generator. To use this in an xslt you need some extension mechanism. With Saxon I simply use the above code and add xmlns:hx="java:exam" to the xsl:stylesheet element: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:hx="java:exam" version="1.0"> (assuming to use the anonymous package and to store the above java source in a file named exam.java, that will be compiled in exam.class). Then, I use <xsl:sort select="hx:hx(.,$seed)" data-type="number"/> where $seed is a parameter I pass to the processor on the command line. Hope this helps... Massimo XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] How to access to a node c, Oliver Becker | Thread | [xsl] Variables in HTML tags, noam |
[xsl] Lookup (?), Kurt George Gjerde | Date | Re: [xsl] alternate row color in a , Jeni Tennison |
Month |