Re: [xsl] Random selection

Subject: Re: [xsl] Random selection
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sun, 30 Nov 2008 22:08:59 +0530
If you are working in Java environment, you could use the Java class
'Random' for random number support.


Supposing your input XML is (this is just an example for illustration.
your real XML could be different.):

  <image val="a.jpg" />
  <image val="b.jpg" />
  <image val="c.jpg" />
  <image val="d.jpg" />
  <image val="e.jpg" />
  <image val="f.jpg" />

The stylesheet could be:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="";
  <xsl:output method="html" />

  <xsl:template match="/">
    <xsl:variable name="max" select="count(images/image)" />
    <xsl:variable name="random" select=""/>
    <xsl:variable name="x" select="java:nextInt($random, $max) + 1" />
<!-- this is the random number -->
        <img src="{images/image[$x]/@val}" />


Here I have used the Java extension mechanism provided by Xalan.

On Sun, Nov 30, 2008 at 8:43 PM, sudheshna iyer <sudheshnaiyer@xxxxxxxxx> wrote:
> Here is the more clear explanation of what I am looking for:
> I need to select an image from pool of images randomly and produce <img src> element of html.
> This xsl should read the image tags from xml and based on the number of image tag nodes in the xml, it
> should display random image by producing <img src> element of html.
> Thank you for your help.

Mukul Gandhi

Current Thread