Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old May 1st, 2006, 03:10 PM
Registered User
 
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

 
Old May 1st, 2006, 04:28 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old May 2nd, 2006, 05:22 AM
Authorized User
 
Join Date: Apr 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vakorde
Default

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?


 
Old May 2nd, 2006, 05:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old May 2nd, 2006, 08:03 AM
Registered User
 
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot Michael, just one question, what would be a good value for the "seed" parameter of the random:random-sequence function?

Bernardo

 
Old May 2nd, 2006, 08:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old May 2nd, 2006, 09:27 AM
Registered User
 
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old May 2nd, 2006, 10:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old May 2nd, 2006, 10:26 AM
Registered User
 
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:D I don't think that's an option right now but thanks Michael, I'll figure something out.

 
Old September 30th, 2012, 06:28 AM
Registered User
 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic sort order or sort datatype kapy_kal XSLT 2 September 18th, 2007 02:10 PM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
Random Numbers not so random... katsarosj ASP.NET 1.0 and 1.1 Basics 5 November 20th, 2003 12:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.