[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

FW: random output from a xml simple document

Subject: FW: random output from a xml simple document
From: "Jarno Elovirta" <jarno@xxxxxxxxxxxxxx>
Date: Thu, 28 Dec 2000 13:50:55 +0200
java nextdouble
Hip hei!

> XSL processor = Xalan
> how can I output (html)"just one flower" randomly any time?
> from a xml simple document like this
>
> <plants>
>   <flower>Magnolia</flower>
>   <flower>Iris</flower>
>   <flower>Sunflower</flower>
>   <flower>Petunia</flower>
>   <flower>Daisy</flower>
>   <flower>Coliflor</flower>
>   <flower>Rosa</flower>
>   <flower>Patata</flower>
> </plants>

Did you have something like this in mind?

[c:\temp]type test.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<plants>
  <flower>Magnolia</flower>
  <flower>Iris</flower>
  <flower>Sunflower</flower>
  <flower>Petunia</flower>
  <flower>Daisy</flower>
  <flower>Coliflor</flower>
  <flower>Rosa</flower>
  <flower>Patata</flower>
</plants>

[c:\temp]type test.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:java="http://xml.apache.org/xslt/java"
                exclude-result-prefixes="java">

<xsl:output method="html"
            indent="yes"/>

<xsl:template match="/">
  <html>
    <head>
      <title />
    </head>
    <body>
      <xsl:variable name="R" select="java:java.util.Random.new()" />
      <xsl:variable name="r" select="ceiling(count(plants/flower) *
java:nextDouble($R))" />
      <xsl:value-of select="plants/flower[$r]" />
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>

[c:\temp]xalan2-j -in test.xml -xsl test.xsl -ts
Line #16, Column #69: variable, select='java:java.util.Random.new()':
java.util.Random@3c1b02
Line #17, Column #93: variable, select='ceiling(count(plants/flower) *
java:nextDouble($R))': 4
Line #18, Column #50: value-of, select='plants/flower[$r]':
     org.apache.xalan.stree.ElementImpl@6f9157
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>Petunia</body>
</html>

Seems to work with both Xalan-J and Xalan2-J. There is a (astronomically
small) possibility that nextDouble() will return 0, in which case
plants/flower[$r] will return an empty node list, so you should adjust the
scale to [java.lang.Double.MIN_VALUE,1] - or just use plants/flower[1] when
$r = 0.

Jarno - Funker Vogt: Subspace


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.