Getting random child node Options
azzlack
Posted: Tuesday, February 19, 2008 1:02:10 PM

Rank: Devotee

Joined: 8/5/2007
Posts: 59
Location: Bergen, Norway
Is there a way to get a random subnode in Umbraco?

I have this XSLT:
Code:

<a href="{umbraco.library:NiceUrl(@id)}">
<img alt="{@nodeName}">
<xsl:attribute name="src">
      /umbraco/ImageGen.aspx?image=
      <xsl:value-of select="umbraco.library:GetMedia(./node/data [@alias = 'FullImage'], 'false')/data [@alias = 'umbracoFile']"/>
&amp;height=125&amp;width=125&amp;Pad=true
</xsl:attribute>
</img>
</a>


What I need is to get a random media file inside the gallery as a thumbnail.
The above code works, but only gets the first node.

www.eyecatch.no
drobar
Posted: Tuesday, February 19, 2008 2:59:43 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,410
Location: KY, USA
If, like me, you keep all your site's banner images in a single area of the content tree, you can use the following XSLT to select a random image to display:

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 Exslt.ExsltMath">


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

<xsl:param name="currentPage"/>
<xsl:variable name="bannerFolder" select="1205"/>

<!-- ============================================================= -->

<xsl:template match="/">
    <xsl:variable name="numNodes" select="count(umbraco.library:GetXmlNodeById($bannerFolder)/node)"/>
    <xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>

    <img alt="Our Banner Image">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetXmlNodeById($bannerFolder)/node [position() = $random]/data [@alias='bannerImage']"/>
        </xsl:attribute>
    </img>
</xsl:template>

<!-- ============================================================= -->

</xsl:stylesheet>


You'll notice that I used the Exslt.ExsltMath:random() function. You can also create an inline function of your own in C# or VB.NET or JavaScript if you need even more flexibility.

cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
azzlack
Posted: Wednesday, February 20, 2008 2:07:28 PM

Rank: Devotee

Joined: 8/5/2007
Posts: 59
Location: Bergen, Norway
How can I make a variable that gets a random node inside the gallery?

I tried to do this:

Code:

<xsl:variable name="numNodes" select="count(umbraco.library:GetXmlNodeById($currentPage)/node)"/>

<xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>
<xsl:variable name="randomNode" select="umbraco.library:GetXmlNodeById(.)/node [position() = $random]/data [@alias='FullImage']" />
                  
<img alt="{@nodeName}">
      <xsl:attribute name="src">
            /umbraco/ImageGen.aspx?image=
            <xsl:value-of select="umbraco.library:GetXmlNodeById($randomNode)/data [@alias = 'umbracoFile']"/>
            &amp;height=125&amp;width=125&amp;Pad=true&amp;BgColor=Transparent
      </xsl:attribute>
</img>


But it does not work.

I do not have that heavy experience with XSLT, but what I'm trying to do is to select a random node from the gallery and use the media file it links to as thumbnail image.

www.eyecatch.no
azzlack
Posted: Monday, April 14, 2008 12:17:23 PM

Rank: Devotee

Joined: 8/5/2007
Posts: 59
Location: Bergen, Norway
I tried a different way, but it did not work either.

Code:
<xsl:variable name="random" select="floor(Exslt.ExsltMath:random() * $totalRecords) + 1"/>
...
<xsl:value-of select="umbraco.library:GetMedia(./node [position() = $random]/data [@alias = 'FullImage'], 'false')/data [@alias = 'umbracoFile']"/>


I have the following site structure:
Code:
-Home
    -Image Gallery
        -Designs
            -Picture1
            -Picture2
        -Photos
            -Picture1
            -Picture2
            -Picture3

The "Picture" nodes has a document type named "Photo", which has a property named "FullImage" of type Media Picker.

The images themselves are located in various folders in the "Media" tab in Umbraco.

www.eyecatch.no
drobar
Posted: Monday, April 14, 2008 3:14:42 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,410
Location: KY, USA
Ahh, simple enough. The key is that you're using a MediaPicker and my example used an Upload data type. The difference is that with the Upload data type you get the path of the image, but with the MediaPicker you simply get its ID and you have to get the path to the file from there.

You can see this when you look at the img's src= tag in the output to your browser. You probably saw something like src="1353". That's the ID.


Here's how you'd do that based on my first example:
Code:

    <img alt="Our Banner Image">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetXmlNodeById($bannerFolder)/node [position() = $random]/data [@alias='bannerImage']"/>
        </xsl:attribute>
    </img>

...becomes...
Code:

    <xsl:variable name="imageID" select="umbraco.library:GetXmlNodeById($bannerFolder)/node [position() = $random]/data [@alias='bannerImage']"/>
    <img alt="Our Banner Image">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($imageID, 0)/data [@alias='umbracoFile']"/>
        </xsl:attribute>
    </img>


cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
azzlack
Posted: Tuesday, April 15, 2008 1:08:47 PM

Rank: Devotee

Joined: 8/5/2007
Posts: 59
Location: Bergen, Norway
I think that the reason it does not work, is that the xpath is not correct.

The xslt for the page:
Code:

<xsl:template name="listNodes">
<xsl:param name="node"/>

<xsl:for-each select="$currentPage/node [string(./data [@alias='umbracoNaviHide']) != '1']">

<!-- only the nodes for this page -->
        <xsl:if test="position() &gt;= $startMatch and position() &lt;= $endMatch">
            <xsl:call-template name="listNodes">
                <xsl:with-param name="node" select="."/>
            </xsl:call-template>
        </xsl:if>
        </xsl:for-each>

listNodes:
Code:
<xsl:variable name="imageID" select="umbraco.library:GetXmlNodeById($currentPage/node)/node [position() = $random]/data [@alias='FullImage']"/>

<div class="Item">
            <!-- get photo thumbnail -->
            <a href="{umbraco.library:NiceUrl(@id)}">
            <img class="fading">
                <xsl:attribute name="src">
                    /umbraco/ImageGen.aspx?image=
                    <xsl:value-of select="umbraco.library:GetMedia($imageID, 0)/data [@alias='umbracoFile']"/>
                    &amp;height=125&amp;width=125&amp;pad=true
                </xsl:attribute>
            </img>            
            </a>
        </div>


www.eyecatch.no
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.