[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] FW: random output from a xml simple document
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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|