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

Re: question on random numbers for browser XSLT

Subject: Re: question on random numbers for browser XSLT
From: Hermann Stamm-Wilbrandt <STAMMW@xxxxxxxxxx>
Date: Fri, 24 Jul 2009 19:39:09 +0200
Re:  question on random numbers for browser XSLT
Scott,

> To Ben:
> The cache-append idea was my first thought, but I couldn't think of a
good
> way to pull together how to have each request be unique. It's possible
> using node-set(), though:
> ...
> ...
>     <xsl:choose>
>       <xsl:when test="function-available('exsl:node-set')">
>         <xsl:value-of select="document(concat('/cgi-bin/rand.pl?',
$scale,
> '&amp;x=', generate-id(exsl:node-set($seed))))" />
>       </xsl:when>
>       <xsl:when test="function-available('msxsl:node-set')">
>         <xsl:value-of select="document(concat('/cgi-bin/rand.pl?',
$scale,
> '&amp;x=', generate-id(msxsl:node-set($seed))))" />
>       </xsl:when>
> ...

I tried your solution and after replacing '&amp;x=' with '+' it directly
worked on my local web server!
(I did not know that generate-id works in a browser)

But as already posted it did not work after installed on my real webserver
because that does not have enough power to handle the six document opens.

Btw, I really like the solution of David Carlisle to provide exslt:node-set
for IE browsers (and not having to deal with exslt/msxml):
http://dpcarlisle.blogspot.com/2007/05/exslt-node-set-function.html


Mit besten Gr|_en / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschdftsf|hrung: Erich Baier
Sitz der Gesellschaft: Bvblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294



             Hermann
             Stamm-Wilbrandt/G
             ermany/IBM@IBMDE                                           To
                                       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
             07/24/2009 06:11                                           cc
             PM
                                                                   Subject
                                       Re:  question on random
             Please respond to         numbers for browser XSLT
             xsl-list@xxxxxxxx
              lberrytech.com








Hello,

Scott Trenda has problems in getting his email accepted by xsl-list and
asked me to forward his response to Ben's post.

================================================
The XSL-List keeps rejecting my replies despite my best efforts (to prevent
my e-mails from being encoded in base64), so here's my response to Ben's
last e-mail on the issue, and a different approach that would solve the
same-value problem. Cross-post it to the list for official archival if you
wish.

To Ben:
The cache-append idea was my first thought, but I couldn't think of a good
way to pull together how to have each request be unique. It's possible
using node-set(), though:

<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="exsl
msxsl">
  <xsl:output method="html" encoding="UTF-8" />

  <xsl:template match="/">
    <html>
      <body>
        <div>rand(6): <xsl:call-template name="rand" /></div>
        <div>rand(6): <xsl:call-template name="rand" /></div>
        <div>rand(6): <xsl:call-template name="rand" /></div>
        <div>rand(6): <xsl:call-template name="rand" /></div>
        <div>rand(6): <xsl:call-template name="rand" /></div>
        <div>rand(6): <xsl:call-template name="rand" /></div>
      </body>
    </html>
  </xsl:template>

  <xsl:template name="rand">
    <xsl:param name="scale" select="6" />
    <xsl:variable name="seed" />
    <xsl:choose>
      <xsl:when test="function-available('exsl:node-set')">
        <xsl:value-of select="document(concat('/cgi-bin/rand.pl?', $scale,
'&amp;x=', generate-id(exsl:node-set($seed))))" />
      </xsl:when>
      <xsl:when test="function-available('msxsl:node-set')">
        <xsl:value-of select="document(concat('/cgi-bin/rand.pl?', $scale,
'&amp;x=', generate-id(msxsl:node-set($seed))))" />
      </xsl:when>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>


That'll give you six random values, assuming the Perl script recognizes the
&x=Idxxxxx as cache-append garbage.

~ Scott
================================================


Mit besten Gr|_en / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschdftsf|hrung: Erich Baier
Sitz der Gesellschaft: Bvblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294



             Ben Mendis
             <ben@antennahouse
             .com>                                                      To
                                       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
             07/24/2009 03:58                                           cc
             PM
                                                                   Subject
                                       Re:  question on random
             Please respond to         numbers for browser XSLT
             xsl-list@xxxxxxxx
              lberrytech.com










Scott Trenda wrote:
> Out of curiosity, wouldn't the Perl external-document approach be
inaccurate due to the way XSLT function results are (supposed to be)
evaluated and stored? That is, if you had this:
>
> <xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
>   <xsl:output method="html" encoding="UTF-8" />
>
>   <xsl:template match="/">
>     <html>
>       <body>
>         <div>rand(6): <xsl:value-of select="document
('/cgi-bin/rand.pl?6')" /></div>
>         <div>rand(6): <xsl:value-of select="document
('/cgi-bin/rand.pl?6')" /></div>
>         <div>rand(6): <xsl:value-of select="document
('/cgi-bin/rand.pl?6')" /></div>
>         <div>rand(6): <xsl:value-of select="document
('/cgi-bin/rand.pl?6')" /></div>
>         <div>rand(6): <xsl:value-of select="document
('/cgi-bin/rand.pl?6')" /></div>
>         <div>rand(6): <xsl:value-of select="document
('/cgi-bin/rand.pl?6')" /></div>
>       </body>
>     </html>
>   </xsl:template>
>
> </xsl:stylesheet>
>
> Wouldn't that output the same result six times because (theoretically)
the XSLT processor should only have to retrieve the contents of document
('/cgi-bin/rand.pl?6') once, and just use the retrieved content for each
successive call? It is the same URI, after all, but I'm not sure just how
the gears of the big XSLT processors work in this situation.
>
>
You could get around that limitation by adding a counter or some other
unique identifier to the query string so that it looks like a different
document is being requested. On the Perl side, it will just ignore any
erroneous parameters.
> ~ Scott
>
> -----Original Message-----
> From: Hermann Stamm-Wilbrandt [mailto:STAMMW@xxxxxxxxxx]
> Sent: Wednesday, July 22, 2009 7:43 AM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  question on random numbers for browser XSLT
>
>
> I want random numbers in a stylesheet but I am bound to XSLT 1.0 and
> not able to make use of "http://exslt.org/random" extensions because
> the stylesheet should be accessed by <?xml-stylesheet ...?> in a
> browser XML document.
>
> Is there a pure XSLT solution?
> (the quality of the generated random numbers is not that important)
>
>
> After some playing around I found below (not pure XSLT) solution.
> Important is that the PERL script lives in the same domain as the
> stylesheet. You may try it out (random number "0-5") by clicking here:
> http://stamm-wilbrandt.de/en/xsl-list/random/rand6.xml
>
>
> $ cat rand6.xml
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <?xml-stylesheet type="text/xsl" href="rand6.xsl"?>
> <tag/>
>
> $ cat rand6.xsl
> <xsl:stylesheet
>   version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>   <xsl:output method="html" encoding="UTF-8"/>
>
>   <xsl:template match="/">
>     <xsl:variable name="rand"
>       select="document('/cgi-bin/rand.pl?6')"/>
>     <html><body>rand(6): <xsl:value-of select="$rand"/></body></html>:
>   </xsl:template>
>
> </xsl:stylesheet>
> $
>
> $ cat cgi-bin/rand.pl
> #!/usr/bin/perl -w.
>
> print "Content-type: text/xml\n\n";
>
> printf("<?xml version=\"1.0\"?>\n");
> printf("<rand>%d</rand>\n", rand($ARGV[0]));
> printf("<!-- PERL rand(%d) -->\n",$ARGV[0]);
>
>
> Mit besten Gr|_en / Best wishes,
>
> Hermann Stamm-Wilbrandt
> Developer, XML Compiler
> WebSphere DataPower SOA Appliances
> ----------------------------------------------------------------------
> IBM Deutschland Research & Development GmbH
> Vorsitzender des Aufsichtsrats: Martin Jetter
> Geschdftsf|hrung: Erich Baier
> Sitz der Gesellschaft: Bvblingen
> Registergericht: Amtsgericht Stuttgart, HRB 243294
>
>
>
>

--

Ben Mendis
Support Specialist
Antenna House
10410 Kensington Pkwy
Suite 207
Kensington, Maryland 20895
USA
Phone: +1 301-942-4007
Email: ben@xxxxxxxxxxxxxxxx
Web: www.antennahouse.com

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.