I have a question about some sort af random function in XSLT.
I have an XML-file that very simplified look similar to this:
<node id="1198">
<node id="1201">
<data alias="name">Flemming</data>
<data alias="picture">1200</data>
</node>
<node id="1207">
<data alias="name">John</data>
<data alias="picture">1205</data>
</node>
<node id="1208">
<data alias="name">Michael</data>
<data alias="picture">1206</data>
</node>
</node>
I would like to have some XSLT, that ramdomly took one of the nodes id's and put it into a variable called "choosenNode". Like this, if the node with the ID of 1207 was the selected one:
<xsl:variable name="choosenNode" value="1207" />
How can i do this? Is there a random-function in XSLT? By the way, I would like the variable to be refreshed on every page where the XSLT is included.
And I work in Umbraco CMS, if that helps you guys.
Thanks, -Kim
In Umbraco you can do something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltMath">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<!-- This should probably be a macro parameter so you can use this elsewhere-->
<xsl:variable name="parentNode" select="1048"/>
<xsl:template match="/">
<xsl:variable name="numberOfNodes" select="count(umbraco.library:GetXmlNodeById($parentNode)/node)"/>
<xsl:variable name="randomPosition" select="floor(Exslt.ExsltMath:random() * $numberOfNodes) + 1"/>
<xsl:variable name="randomNode" select="umbraco.library:GetXmlNodeById($parentNode)/node [position() = $randomPosition]"/>
<!--
You now have the node in the $randomNode variable
If you just want the id then you can do an XPath query on the variable
or you can modify the XPath above to get the property you are after rather than
the whole node
-->
<xsl:value-of select="$randomNode/@nodeName" />
</xsl:template>
</xsl:stylesheet>
Hope this helps.
Tim
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With