Re: [xsl] Random selection

Subject: Re: [xsl] Random selection
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sun, 30 Nov 2008 09:02:52 -0800
On Sun, Nov 30, 2008 at 8:38 AM, Mukul Gandhi <gandhi.mukul@xxxxxxxxx> wrote:
> If you are working in Java environment, you could use the Java class
> 'Random' for random number support.
>

Hi Mukul,

What about people, who use C#, Python, VB, C++, ..., etc?

I strongly believe that recommending a non-portable solution on this
list when a known, 100% XSLT, portable one exists, should be
discouraged.


-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play



> (http://java.sun.com/javase/6/docs/api/java/util/Random.html).
>
> Supposing your input XML is (this is just an example for illustration.
> your real XML could be different.):
>
> <images>
>  <image val="a.jpg" />
>  <image val="b.jpg" />
>  <image val="c.jpg" />
>  <image val="d.jpg" />
>  <image val="e.jpg" />
>  <image val="f.jpg" />
> </images>
>
> The stylesheet could be:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>                       xmlns:java="http://xml.apache.org/xalan/java";
>                       exclude-result-prefixes="java"
>                       version="1.0">
>
>  <xsl:output method="html" />
>
>  <xsl:template match="/">
>    <xsl:variable name="max" select="count(images/image)" />
>    <xsl:variable name="random" select="java:java.util.Random.new()"/>
>    <xsl:variable name="x" select="java:nextInt($random, $max) + 1" />
> <!-- this is the random number -->
>    <html>
>      <head>
>        <title/>
>      </head>
>      <body>
>        <img src="{images/image[$x]/@val}" />
>      </body>
>    </html>
>  </xsl:template>
>
> </xsl:stylesheet>
>
> 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.
>
>
>
> --
> Regards,
> Mukul Gandhi

Current Thread