Copied to clipboard

Flag this post as spam?

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


  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    May 12, 2011 @ 15:57
    Bjarne Fyrstenborg
    0

    How to display random quote?

    Hi

    I have the following structure of my content:

    - Content

    • Master
      • da
        • Frontpage
        • Subpage
        • Subpage
        • News
          • Quotes
            • - Quote
            • - Quote
    The Quotes and Quote have each a document type "Quotes" and "Quote", where Quote has the properties quoteText and quoteName.
    How can I create my xslt so for each Quote node, which use the Quote document type, it writes the quote text and quote name.. and then display a random node, so I can place the macro on eg. frontpage and a subpage?
    - Bjarne
  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    May 12, 2011 @ 15:59
    Bjarne Fyrstenborg
    0

    The News and Quotes node are at same level.

  • Profiterole 232 posts 264 karma points
    May 12, 2011 @ 16:03
    Profiterole
    0

    Hi, for the random node, it's simple :

    <?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.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:umbraco.contour="urn:umbraco.contour"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets umbraco.contour ">


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

    <xsl:param name="currentPage"/>
        

    <xsl:template match="/">

    <!-- start writing XSLT -->
    <xsl:variable name="randomNode" select="/macro/randomNode/node"/>
    <xsl:value-of select="$randomNode/data[@alias = 'quoteName']" disable-output-escaping="yes" />
    <p><xsl:value-of select="$randomNode/data[@alias = 'quoteText']" disable-output-escaping="yes" />
    </p>


    </xsl:template>

    </xsl:stylesheet>

    For the macro, you name the parameter alias randomNode and choose contentRandom as type.

  • Sebastian Dammark 583 posts 1407 karma points
    May 12, 2011 @ 17:37
    Sebastian Dammark
    2

    Hi Bjarne

    I haven't tested it, but it should work

    <?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:umb="urn:umbraco.library"
        xmlns:emath="urn:Exslt.ExsltMath"
        exclude-result-prefixes="msxml umb emath ">
    
        <xsl:output method="xml" omit-xml-declaration="yes"/>
    
        <xsl:param name="currentPage"/>
        <xsl:variable name="random" select="floor(emath:random() * count($currentPage/ancestor-or-self::Master/descendant-or-self::Quote)) + 1"/>
    
        <xsl:template match="/">
         <xsl:apply-templates select="$currentPage/ancestor-or-self::Master/descendant-or-self::Quote[$random]" />
        </xsl:template>
    
        <xsl:template match="Quote">
            <div id="randquote">
                <xsl:apply-templates select="quoteName[normalize-space()]" />
                <xsl:apply-templates select="quoteText[normalize-space()]" />
            </div>
        </xsl:template>
    
        <xsl:template match="quoteName">
            <h1>
                <xsl:value-of select="." />
            </h1>
        </xsl:template>
    
        <xsl:template match="quoteText">
            <p>
                <xsl:value-of select="." />
            </p>
        </xsl:template>
    
    </xsl:stylesheet>
  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    May 12, 2011 @ 21:57
    Bjarne Fyrstenborg
    0

    I have wrote the following in my XSLT:

    <xsl:template match="/">

    <!-- start writing XSLT -->
      <xsl:variable name="randomNode" select="/macro/randomNode/*"/>
      <xsl:value-of select="$randomNode/quoteName" disable-output-escaping="yes" />
    <p><xsl:value-of select="$randomNode/quote" disable-output-escaping="yes" />
    </p>


    </xsl:template>

    and added a parameter with alias randomNode to the macro, but it doesn't seem to give a output. I'm using Umbraco 4.7 with the new xml schema .. I have tried with /macro/randomNode/* [@isDoc] too, but doesn't work .

    Is something missing?

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    May 12, 2011 @ 22:20
    Bjarne Fyrstenborg
    0

    Hi Sebastian

    Thank you so much for your input :)
    Just what I needed... it now works perfectly as intended.

    Your code works, just my Master doc types actually is called MainMaster.

    <?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:umb="urn:umbraco.library"
            xmlns:emath="urn:Exslt.ExsltMath"
            exclude-result-prefixes="msxml umb emath ">

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

            <xsl:param name="currentPage"/>
            <xsl:variable name="random" select="floor(emath:random() * count($currentPage/ancestor-or-self::MainMaster/descendant-or-self::Quote)) + 1"/>

            <xsl:template match="/">
             <xsl:apply-templates select="$currentPage/ancestor-or-self::MainMaster/descendant-or-self::Quote[$random]" />
            </xsl:template>

            <xsl:template match="Quote">
                    <div id="randquote">
                            <xsl:apply-templates select="quoteText[normalize-space()]" />
                            <xsl:apply-templates select="quoteName[normalize-space()]" />
                    </div>
            </xsl:template>
            
            <xsl:template match="quoteText">
                    <p>
                            <xsl:value-of select="." />
                    </p>
            </xsl:template>
              
            <xsl:template match="quoteName">
                    <b>
                            <xsl:value-of select="." />
                    </b>
            </xsl:template>
            
      

    </xsl:stylesheet>

    So if you take a look at the temporary url here: http://d22299829.u330.surftown.dk/da/ydelser.aspx , then the quote in the left sidebar on subpages change when you refresh the page or go to another page..

    Once again I appreciate your help..

    - Bjarne

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 12, 2011 @ 23:16
    Jan Skovgaard
    0

    Hi Sebastian

    Would you mind sharing this in the wiki if it does not already exist in there? Nice solution indeed :-)

    /Jan

  • Sebastian Dammark 583 posts 1407 karma points
    May 13, 2011 @ 10:35
    Sebastian Dammark
    1

    @Jan and @Bjarne

    Here you go: http://our.umbraco.org/wiki/how-tos/xslt-useful-tips-and-snippets/xslt-getting-a-random-document-when-the-page-reloads

    I've optimized it a bit using root instead of the MainMaster doctype.

     

Please Sign in or register to post replies

Write your reply to:

Draft