XSLT get random node based on data contained in the Node Options
jbrisko
Posted: Monday, September 29, 2008 6:54:16 PM
Rank: Enthusiast

Joined: 8/19/2008
Posts: 40
Location: Utah, USA
Hey guys,
I have a setup where my company is hosting a product information catalog through Umbraco, there is no e-commerce however.

The nodes are setup like this.....

Code:

PRODUCTS
        A
                  Item 1
                  Item 2
                  Item 3
        B
                  Item 4
                  Item 5
                  Item 6
        C
                  Item 7
                  Item 8
                  Item 9
        D
                  Item 10
                  Item 11
                  Item 12
        etc.

(BTW I know code probably isn't the best way to show this table, but I don't know a better way on these forums. If you can let me in on that secret, that would be great. Thanks)

I need to cycle through all of those Item nodes, and find out in each item if the item is new, and then from there, randomly select one of those item nodes, and output data from it on my homepage. What is the easiest and most efficient solution to this problem, there are circa 2000 items involved.

Thanks,
Jeremy
jbrisko
Posted: Monday, September 29, 2008 7:47:24 PM
Rank: Enthusiast

Joined: 8/19/2008
Posts: 40
Location: Utah, USA
Hey Guys,
I actually got this working quite well. I haven't put any details in, but you can see how to access the data where I access the nodeName. Hope this helps someone in the future.

Code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<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">


<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>

<xsl:template match="/">

    <xsl:variable name="ProductRoot" select="umbraco.library:GetXmlNodeById(2032)" />
    
    <xsl:variable name="ProductNodeSet" select="$ProductRoot/descendant::node[data [@alias = 'prodcat_isNew'] = 1]" />
    
    <xsl:variable name="numNodes" select="count($ProductNodeSet)" />
    <xsl:variable name="RandomID" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>
    <xsl:for-each select="$ProductNodeSet[position() = $RandomID]">
        <xsl:value-of select="@nodeName" />
    </xsl:for-each>

</xsl:template>

</xsl:stylesheet>


I found how to get a random ID thanks to Drobar's post here.....
http://forum.umbraco.org/yaf_postst3126_Getting-random-child-node.aspx

Regards,
Jeremy
pkoutoul
Posted: Monday, November 17, 2008 3:41:18 PM

Rank: Fanatic

Joined: 8/9/2007
Posts: 284
Location: Kentucky, USA
Thanks for sharing this code, Jeremy. I was searching for this exact thing. Works perfectly!

Pete Koutoulas • Fayette County Public Schools • Lexington, Kentucky
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.