Re: Another newbie post

Mike Brown <[email protected]>
Newsgroups gmane.text.xml.xslt.extensions
Message-ID <[email protected]>
Justin Morgan wrote:
> You are right - I have tried it with a different XSLT processor and it 
> seems to work (although it does generate the same numbers every time). 
> Thank you.

Random number generation requires a physical source of entropy, e.g. a piece 
of physical hardware exposed in the OS and XSLT processor's programming 
language APIs. Pseudo-random number generation requires one or more seed 
values, e.g. as might be obtained from a clock. It is possible that Xalan's 
implementation requires a seed argument (which means it is noncompliant). Try 
giving it a number. If it works, then maybe you can use the XSLT function 
system-property() to test whether you're using Xalan, and if you are, feed it 
the extra argument?

In general, though, the result of evaluating any XPath function call should, 
in theory, be expected to be the same, when given the same set of arguments. A 
function call in an expression is, as in math, just a representation of a 
relationship between its arguments and the value that it can be evaluated to. 
If you expect a different result every time, then there must be implicit extra 
arguments, perhaps dependent on processor or machine state, that represent the 
variable factors.

In practice, side-effects are not uncommon in extension functions and 
extension elements, and EXSLT does not prohibit them, so you end up with 
processor state being affected by certain expressions & instructions (aside 
from what's expected as part of the XSLT template processing model). In turn, 
this state can affect the evaluation of certain expressions & instructions. 
Processors also rarely cache the result of function evaluation. In a sense, 
this makes things work more like you expect them to, but since XSLT was 
designed with lofty goals of side-effect-free purity in mind, you have to 
expect that maybe it won't always be that way. This may or may not explain
the identical results you're seeing with each run.

The shortcomings of math:random() were discussed here and addressed not too 
long ago (not too long ago by this list's standards, anyway) and resulted in 
the random:random-sequence() function: 
http://exslt.org/random/functions/random-sequence/random.random-sequence.html
Does Xalan support it yet?

Another option for you is to use Xalan's general extension mechanism for 
hooking into Java classes. You can create a java.util.Random object perhaps.

-Mike
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.