xsl-list
[Top] [All Lists]

Re: [xsl] question on random numbers for browser XSLT

2009-07-24 11:26:24
Thanks for the replies,

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.

you are both right.
The stylesheet above generated 6 times the same random number.

Then I modified the URLs to "...?6 a'", "...?6 b'", ... "...?6 f'" and it
worked fine on my local web server (127.0.0.1) -- it generated six random
numbers which might be equal or different.

After uploading this to my web server I realized that sometimes 2,
sometimes>
3 random numbers got created, never 6 -- the web server where my domain is
hosted seems not to be powerful enough to handle the six document
requests :-(>

Then I created nrand.pl which gives mit n random numbers. This is used by
modified stylesheet nrand67.xsl referenced by nrand67.xml, you may try it:
http://stamm-wilbrandt.de/en/xsl-list/random/nrand67.xml


$ cat nrand67.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="nrand67.xsl"?>
<tag/>
$ cat nrand67.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="/">
    <html>
      <body>
        <xsl:for-each select="document('/cgi-bin/nrand.pl?6
+7')/nrand/rand">
          <div>nrand(6): <xsl:value-of select="." /></div>
        </xsl:for-each>:
      </body>:
    </html>
  </xsl:template>
</xsl:stylesheet>

$ cat ../cgi-bin/nrand.pl
#!/usr/bin/perl -w

print "Content-type: text/xml\n\n";

printf("<?xml version=\"1.0\"?>\n");

printf("<nrand>\n");
for($i=0; $i<int($ARGV[1]); $i++)
{
  printf("  <rand>%d</rand>\n", rand(int($ARGV[0])));
}
printf("</nrand>\n");
printf("<!-- PERL rand(%d,%d) -->\n",int($ARGV[0]),int($ARGV[1]));
$


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
Geschäftsführung: Erich Baier
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294


                                                                       
             Ben Mendis                                                
             <ben(_at_)antennahouse                                         
             .com>                                                      To
                                       
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com 
             07/24/2009 03:58                                           cc
             PM                                                        
                                                                   Subject
                                       Re: [xsl] question on random    
             Please respond to         numbers for browser XSLT        
             xsl-list(_at_)lists(_dot_)mu                                       
  
              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(_at_)de(_dot_)ibm(_dot_)com]
Sent: Wednesday, July 22, 2009 7:43 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] 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
Geschäftsführung: Erich Baier
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--

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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>