I have some quotes listed in my content structure..
On each page I want to pick the nodes which should appear on the current page.. but of the nodes picked with multi-node tree picker a random of the selected quotes should be displayed.
<xsl:template match="/">
<!-- Pick a random box index -->
<xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * count($boxIds)) + 1"/>
<!-- Process the one - this gets us into the Umbraco XML doc -->
<xsl:apply-templates select="$boxIds[position() = $random]" />
</xsl:template>
<!-- Simple 'redirect' to the referenced node -->
<xsl:template match="nodeId">
<!-- id() works because we are now inside the Umbraco XML -->
<xsl:apply-templates select="id(.)" />
</xsl:template>
<!-- Template for the Quote -->
<xsl:template match="Quote">
<!-- Do something -->
</xsl:template>
The difference between id() and GetXmlNodeById() is that id() will return an empty set if the picked node has been deleted (it's also a native XPath function), where GetXmlNodeById() will return an error node (or something) that you will then need to error check.
The tricky part is that id() will only find a node inside the document that contains the current context node - a concept that's also hard to explain in Umbraco's specific environment - I tried here: https://gist.github.com/871656
So by applying templates for the randomly selected <nodeId> element, we get "into" the Umbraco XML and from there can just use the id() function and not worry about error messages for deleted nodes.
I know it's a completely different way of thinking, in terms of what most people are used to, but using XSLT it just makes so much more sense.
Get random node of the ones picked with MNTP
Hi..
I have some quotes listed in my content structure..
On each page I want to pick the nodes which should appear on the current page.. but of the nodes picked with multi-node tree picker a random of the selected quotes should be displayed.
How can I set the random Quote node of the selected in the apply-templates?
/Bjarne
Hi Bjarne,
Here's a way to do that:
The difference between id() and GetXmlNodeById() is that id() will return an empty set if the picked node has been deleted (it's also a native XPath function), where GetXmlNodeById() will return an error node (or something) that you will then need to error check.
The tricky part is that id() will only find a node inside the document that contains the current context node - a concept that's also hard to explain in Umbraco's specific environment - I tried here: https://gist.github.com/871656
So by applying templates for the randomly selected <nodeId> element, we get "into" the Umbraco XML and from there can just use the id() function and not worry about error messages for deleted nodes.
I know it's a completely different way of thinking, in terms of what most people are used to, but using XSLT it just makes so much more sense.
/Chriztian
Hi Chriztian
Thanks, it seems to do it :)
I have done something similar before, but where I just get a random node of a nodeset.
So what you get from the code above is a random node id and from that get the xml?
This is the code I ended up with:
/Bjarne
is working on a reply...