XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 14th, 2004, 02:39 PM
echo echo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Posts: 16 echo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 56 sec
Reputation Power: 0
Random XML node via XSL maybe?

Hello to all,

I had problem with selecting one random node from XML via XSL. Some of forum users told me that Xpath don't have random function, so it must be done via some other language in XSL.

I think that it can be done with combination of javascript, so I came up with some code which doesn't work for some reason, so I would need some help :-)

so ...

before foreach:

Code:
<![CDATA[
<script language="javascript">
     function rand(nodeset) {
       return Math.round( Math.random() * (nodeset.length + 1) ) + 1;
     }
</script>
]]>


and start of foreach:

Code:
<xsl:for-each select="channel/item[position()<=1]">
<xsl:sort select="math:rand()" />



Limiting foreach to 1 node works, just still need to make it work "1 random node".

thx in advance,

Echo

Reply With Quote
  #2  
Old August 15th, 2004, 04:08 AM
kid23 kid23 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 62 kid23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Hmm.. I think your problem is more complex than you believe it is..

My first question would be : what do you intend to do with the node that you randomly select (say using javascript) ?

If you plan to use it in the transformation, then forget it, it's impossible, because JS is code executed when the page is running, ie: AFTER it has been transformed using XML+XSL.

On the other hand, if this is just for display, then a few lines of code can help. I suggest you have a look at my answer in this post:

http://forums.devshed.com/t173786/s.html

It's a bit long, but it shows how to use JS & XMLDOM, and if you understand well what I tried to achieve, you'll probably be able to use my solution for you problem... adding some JS code to randomly select a node will not be a big issue then.

Ask me if you need more details.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Random XML node via XSL maybe?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT