Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Profiterole 232 posts 264 karma points
    Jun 08, 2010 @ 17:27
    Profiterole
    0

    Get random node by nodeTypeAlias

    Hi, my tree look like this :

    folder 1
    - subfolder 1
    --- file 1
    --- file 2
    - subfolder 2
    --- file 3
    --- file 4

    And I like to get a random file, not the folder or subfolder. So I created a doctype 'multimedia_file'. Here's the xslt that does not work. I got an error : "Expression must evaluate to a node-set."

        <xsl:param name="parentNode" select="1167" />
        <xsl:variable name="nodeChoisies" select="$parentNode/descendant::node[nodeTypeAlias = 'multimedia_file']" />
        <xsl:variable name="numberOfNodes" select="count(umbraco.library:GetXmlNodeById($nodeChoisies))"/>
        <xsl:variable name="randomPosition" select="floor(Exslt.ExsltMath:random() * $numberOfNodes) + 1"/>
        <xsl:variable name="randomNode" select="umbraco.library:GetXmlNodeById($parentNode)/node [position() = $randomPosition]"/>

    <xsl:template match="/">
    use of randomNode

    Thank you for your help!

  • Stefan Kip 1606 posts 4098 karma points c-trib
    Jun 08, 2010 @ 18:15
    Stefan Kip
    0

    I use this:

    <xsl:variable name="CarsNodeSet" select="umbraco.library:GetXmlNodeByXPath('.')/descendant-or-self::node[@nodeTypeAlias = 'cars']/node" />
      <xsl:variable name="numNodes" select="count($CarsNodeSet)" />
      <xsl:variable name="RandomID" select="floor(Exslt.ExsltMath:random() * $numNodes) + 1"/>
      <xsl:for-each select="$CarsNodeSet[position() = $RandomID]">
  • Jeff Grine 149 posts 189 karma points
    Jun 08, 2010 @ 18:20
    Jeff Grine
    0

    I think it would go more like this?

    <xsl:param name="parentNode" select="1167" />
      <xsl:variable name="nodeChoisies" select="$parentNode/descendant::node[@nodeTypeAlias = 'multimedia_file']" />
       
    <xsl:variable name="numberOfNodes" select="count($nodeChoisies)"/>
       
    <xsl:variable name="randomPosition" select="floor(Exslt.ExsltMath:random() * $numberOfNodes) + 1"/>
       
    <xsl:variable name="randomNode" select="$nodeChoisies [position() = $randomPosition]"/>

     

     

     

    The missing @ in front of nodeTypeAlias is what is likely giving you the error.

     

     

     

     

  • Profiterole 232 posts 264 karma points
    Jun 08, 2010 @ 19:48
    Profiterole
    0

    Ok, the code Jeff gave me bring the same error, so I tried kipusoep code and it almost works like I want. I changed the last line to have only one result :

      <xsl:variable name="randomNode" select="umbraco.library:GetXmlNodeById($parentNode)//node [position() = $RandomID]"/> 

    But, how can I test if my randomNode has nodeTypeAlias = 'multimedia_file' before displays it?

     

  • Douglas Robar 3570 posts 4671 karma points MVP ∞ admin c-trib
    Jun 08, 2010 @ 20:03
    Douglas Robar
    0

    Here's how I do it, along with a check for giving an alternate message if there is nothing to display.

    <?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:template match="/">
    <!-- SELECT THE DOCUMENT TYPE ALIAS TO SEARCH FOR -->
    <xsl:variable name="documentTypeAlias" select="'multimedia_file'"/>

    <!-- GET ALL NODES OF THE SELECTED DOCUMENT TYPE ALIAS -->
    <xsl:variable name="allIds">
    <xsl:for-each select="$currentPage/ancestor-or-self::node[@level=1]//node[@nodeTypeAlias=$documentTypeAlias]">
    <xsl:copy-of select="." />
    </xsl:for-each>
    </xsl:variable>

    <!-- COUNT THE NUMBER OF NODES -->
    <xsl:variable name="numItems" select="count(msxml:node-set($allIds)/node)" />

    <xsl:choose>
    <xsl:when test="$numItems > 0">
    <!-- SELECT A RANDOM NODE -->
    <xsl:variable name="randomNumber" select="floor(Exslt.ExsltMath:random() * $numItems) + 1"/>
    <xsl:variable name="randomItem" select="msxml:node-set($allIds)/node [position() = $randomNumber]" />

    <!-- NOW THAT YOU HAVE A RANDOM ITEM, YOU CAN DISPLAY IT -->
    <xsl:value-of select="$randomItem/@nodeName" />
    </xsl:when>
    <xsl:otherwise>
    <p>No item found</p>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>

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

    </xsl:stylesheet>

    cheers,
    doug.

  • Profiterole 232 posts 264 karma points
    Jun 09, 2010 @ 14:48
    Profiterole
    0

    Thank you Douglas, it works like a charm!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies