 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 1st, 2006, 03:10 PM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
random sort
Hi everyone,
I have been looking everywhere for a solution to this problem without much success. Hope someone here can help me.
I have xml that looks like this:
Code:
<articles>
<article>office</article>
<article>cook</article>
<article>book</article>
<article>water</article>
<article>phone</article>
<article>poster</article>
<article>screen</article>
<article>mouse</article>
<article>paper</article>
<article>key</article>
</articles>
Is there a way to output a list of <article>s in a random order? I'm using version 1.0.
Thanks a lot.
|
|

May 1st, 2006, 04:28 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Find out whether there is an implementation of the random:random-sequence function described at http://www.exslt.org/random/index.html available for your chosen XSLT processor. If there is, you can do a random sort by assigning a random sequence of sufficient length to the variable $rand, and then using
<xsl:sort select="$rand[count(current()/preceding-sibling::*)]"/>
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

May 2nd, 2006, 05:22 AM
|
|
Authorized User
|
|
Join Date: Apr 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
In addition to the same.... is there any way to show the values in the reverse order like
key
paper
mouse
screen
poster
phone
water
book
cook
office
i.e. in reverse order.
Is this can be solved using count() function?
|
|

May 2nd, 2006, 05:27 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Use
<xsl:sort select="position()" data-type="number" order="descending"/>
Or in XSLT 2.0, don't sort at all, just use the reverse() function.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

May 2nd, 2006, 08:03 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks a lot Michael, just one question, what would be a good value for the "seed" parameter of the random:random-sequence function?
Bernardo
|
|

May 2nd, 2006, 08:43 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
If you want the same random sequence every time, something like 0.2349712361237 or wherever your fingers take you. If you want a different sequence each time, pick up something from the system clock.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

May 2nd, 2006, 09:27 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm using Xalan and I can't seem to find an implementation of the random:random-sequence function. Is there any other way of doing this?
|
|

May 2nd, 2006, 10:01 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Yes: you can switch to a processor that does implement it, for example Saxon. ;)
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

May 2nd, 2006, 10:26 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
:D I don't think that's an option right now but thanks Michael, I'll figure something out.
|
|

September 30th, 2012, 06:28 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, I have similar problem. I have XML with questions and answers with structure like this:
Code:
<RECORD>
<count Type="Long Integer">1</count>
<question Type="Memo">What is my name?</question>
<answer Type="Memo">Mio</answer>
<answer Type="Memo">Joseph</answer>
<answer Type="Memo">Peter</answer>
<answer Type="Memo">John</answer>
</RECORD>
I need to mix answers in question. Now, the right answer is always first, but i need to sort it randomly. Is it easily possible with XSLT 2.0? I would need to have different sort for each RECORD. Thanks
|
|
 |