|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Random XML node via XSL maybe? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|