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 October 17th, 2007, 09:10 AM
Authorized User
 
Join Date: Jul 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to carlosaml
Default Select nodes randomly

Hi all

I need help to extract some information from my xml randomly...

I have a structure like this:

<country id="1">
<page id="1">
<banner id="1">
<product id="1">...</product>
<product id="2">...</product>
</banner>
<banner id="2">
<product id="3">...</product>
<product id="2">...</product>
</banner>
</page>
<page>...</page>


What I need to do is, for each page, select only one banner containing only product, randomly...
Is it possible to do with xslt?

Thanks.

 
Old October 17th, 2007, 09:18 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

XSLT is as a language is defined so that if you run the stylesheet twice with the same parameters you should get the same output, therefore is doesn't contain any 'random()' type functions.

Depending on what language you are calling from you could pass in a random number as a parameter and use something like:

match="banner[position()=($param mod last())]"


/- Sam Judson : Wrox Technical Editor -/
 
Old October 17th, 2007, 10:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's no random number function in XSLT, though there is one in the EXSLT library (www.exslt.org) which may be available for your processor. If not, you'll have to implement your own extension function, or pass a random number in as a parameter. It depends a bit what you want to achieve - if the same transformation is done twice, do you want the same random number or a different random number?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 17th, 2007, 10:18 AM
Authorized User
 
Join Date: Jul 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to carlosaml
Default

Quote:
quote:Originally posted by mhkay
 There's no random number function in XSLT, though there is one in the EXSLT library (www.exslt.org) which may be available for your processor. If not, you'll have to implement your own extension function, or pass a random number in as a parameter. It depends a bit what you want to achieve - if the same transformation is done twice, do you want the same random number or a different random number?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
To Select a specific nodes from XML vkommera XSLT 4 June 27th, 2008 03:39 PM
Session Variables Randomly Disappeared Dmitriy .NET Framework 1.x 1 November 30th, 2006 01:24 AM
How to select these nodes? hustsay23 XSLT 1 September 7th, 2006 04:15 AM
Help: Select every node except these nodes arcuza XSLT 8 May 25th, 2005 08:07 AM
Randomly choose Operator from Array interrupt Javascript How-To 2 April 9th, 2005 07:49 AM





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