Le Mardi, 14 oct 2003, � 13:34 Europe/Zurich, Jon Evans a �crit :
> ...I can use a directory generator to give me a list of images, but I > don't know how to transform that list in order to select one of them > at random.
The easiest thing that comes to mind is an XSLT transform, using java extensions to call java.util.Random to generate a random selection index.
Thanks Bertrand, I used this suggestion and it works. Here's how I did it:
<map:pipeline type="noncaching">
<map:match pattern="random-image">
<map:generate type="directory" src="resources/images/random"/>
<map:transform src="resources/xsl/randomimage.xsl"/>
<map:serialize type="xml"/>
</map:match>
</map:pipeline>[...]
<map:match pattern="*">
<map:aggregate element="document">
<map:part src="cocoon:/header" element="header"
strip-root="true" />
<map:part src="cocoon:/page/{0}" element="content"
strip-root="false" />
<map:part src="cocoon:/leftnav" element="leftnav"
strip-root="true" />
<map:part src="cocoon:/footer" element="footer"
strip-root="true" />
<map:part src="cocoon:/random-image" element="random-image"/>
</map:aggregate>
<map:transform src="main.xsl">
<map:parameter name="contextPath"
value="{request:contextPath}" />
</map:transform>
<map:serialize type="html"/>
</map:match>
randomimage.xsl: <?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" xmlns:dir="http://apache.org/cocoon/directory/2.0" exclude-result-prefixes="java dir">
<xsl:template match="dir:directory">
<xsl:variable name="random" select="java:java.util.Random.new()"/>
<xsl:variable name="max" select="count(dir:file) - 1"/>
<xsl:variable name="rnd" select="java:nextInt($random, $max) + 1"/> <filename><xsl:value-of select="@name"/>/<xsl:value-of
select="dir:file[$rnd]/@name"/></filename>
</xsl:template>
</xsl:stylesheet>
fragment in main.xsl: <xsl:template match="random-image"> <img class="random" src="images/{/document/random-image/filename}" alt="" /> </xsl:template>
Now, when one of my "cocoon:/page/{0}" files contains the tag <randomimage/>, it is replaced with an img tag as above.
It works, but what do you think? I think including it in the agregate document looks a bit hacky.
Jon
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
