xsl-list
[Top] [All Lists]

RE: Creating an array

2003-05-27 06:27:16
[Alan Gardner]

I'm trying to create the ability to select nodes at random 
(easy enough),
however I'm getting duplicates. I've tried to create a string 
(an array if
you will) of numbers that I generated that I can check each 
time I loop
through the nodes, but if it is possible, it's outside of my 
current xsl
understanding.

Is it possible to build a delimited string/array? Or does anyone have
suggestions on how to eliminate duplicates while generating 
random numbers
in the first place?


Well, you cannot build up a global variable (or any variable) as you go
since they cannot be changed after creation.  So you would have to use
some kind of recursion and pass the previously-selected nodes in a
parameter.  At some point you would come up against the limits on
recursion.

Leaving aside recursion limits, for each node you randomly select you
could add the value of its generate-id() to a space-separated list of
previous generate-id() values.  You would use contains() to see if you
had already selected a node.

If you know you will only randomly select a certain maximum number of
nodes, then you could create a list of random numbers in that range
outside of xslt and put them into a lookup element in the stylesheet.  I
used this method once - I had to dither the position of some (x,y)
points that would have been on top of each other otherwise (for an svg
output), but I did not have to bother about duplicates.

If you go beyond xslt 1.0, you may be able to build up your list in a
variable and then convert it to a nodeset (I say "may" because whether
it will work depends on the size of the nodeset and the processor - I
have hit strange limits a few times).  This could give you more
flexibility.

I imagine that Mike Kay or Jeni will tell us how xslt2 will make it
easier.

Cheers,

Tom P

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



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