Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I generate random numbers with XPATH?

XPATH

How do I generate random numbers with XPATH?

by  flumpy  Posted    (Edited  )
You would have to extend xpath functions to generate a random number.. something like this:

<xsl:stylesheet version="1.0" xmlns:inatos="uri://none" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:msxsl="urn:schemas- microsoft-com:xslt">

<msxsl:script language="JavaScript" implements-prefix="inatos">
<![CDATA[
function rand()
{
return "" + Math.random();
}
]]>
</msxsl:script>

<xsl:template match="/">
<xsl:value-of select="inatos:rand()"/>
</xsl:template>
</xsl:stylesheet>

Heres something to do too, try to make the function take an argument from a node value , and seed the random number :) Hint: you are passing a node to the function, and therefore have to treat it like a node, not an int!

This only works with MSXML, and you would have to find other ways of doing with other engines...

There are some good pre-made packages to do this sort of thing for other engines at http://www.exslt.org/math/
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top