How to select a random node from external XML
This is an open discussion with 28 replies, filed under XSLT.
Search
Funny, it's the same number in my Terminal:
imacoda:Desktop me$ xsltproc style.xsl data.xml <?xml version="1.0"?> 0.394613364429499 imacoda:Desktop me$ xsltproc style.xsl data.xml <?xml version="1.0"?> 0.394613364429499 imacoda:Desktop me$ xsltproc style.xsl data.xml <?xml version="1.0"?> 0.394613364429499
In my (Mac) terminal math:random
always outputs the same number. But this number depends on where the function is called. If I try this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:math="http://exslt.org/math" extension-element-prefixes="exsl math"> <xsl:output method="xml" indent="yes" /> <xsl:template match="/"> <xsl:value-of select="math:random()"/> <xsl:text> </xsl:text> <xsl:call-template name="testme"/> </xsl:template> <xsl:template name="testme"> <xsl:value-of select="math:random()"/> </xsl:template> </xsl:stylesheet>
the output is:
imacoda:Desktop me$ xsltproc style.xsl data.xml <?xml version="1.0"?> 7.82636925942561e-06 0.131537788143166 imacoda:Desktop me$ xsltproc style.xsl data.xml <?xml version="1.0"?> 7.82636925942561e-06 0.131537788143166 imacoda:Desktop me$ xsltproc style.xsl data.xml <?xml version="1.0"?> 7.82636925942561e-06 0.131537788143166
So I guess that there is indeed a problem in the implementation.
It seems so. If I call the function twice in the same transformation, I get two different numbers. I wonder where I could log this as an issue :S
Interesting. After having had a deeper look at the libxslt code I found out that math:random
is missing its seed. Apparently the libxslt mailing list had a discussion about it in 2008 but never pursued or patched it.
I am not really sure why the random numbers on my machine looked random before... simply value-offing math:random always produces the very same number.
Also, it is noteworthy that development on libxslt is not dead yet. I'll try come up with a solution and post a patch to the mailing list; let's see what happens. :-)
Awesome dude.
Alright, I've posted my patch.
And BAM, I have cemented myself in the very heart and soul of the XSLT you all are using! :-D
Nils, you're now open source famous.
Create an account or sign in to comment.
And here are my results:
The number is the same every time... Weird.