Copied to clipboard

Flag this post as spam?

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


  • Andrew Blackmore 84 posts 127 karma points
    Apr 05, 2010 @ 22:04
    Andrew Blackmore
    1

    Getting attribute from another node

    Hey All,

    I'll start by saying this is either a real brain buster, or I'm stupid :)

     

    I've been working on this for quite a long time now. I have 6 blogs set up on my company's site (not yet live thank god). I have almost everything that NEEDS to be working all set. There is sort of a 'blog network' homepage that has the most recent posts from every blog. Sounds simple enough. That part was. The hangup is in a small design element.

     

    We have a large date next to every entry. The date should not show up again if the following post has the same date. I tried setting this up so the preceding posts' date was compared to the current post and if they matched, I didn't have the date show up. The issue I have run into is getting the createDate or postedDate (attribute I added for back-dating posts) from that preceding node. They only share the root node as a parent so the structure is this

     

    Home

    -BlogA

    --Year

    ---Month

    ----Day

    -----PostA

    -BlogB

    --Year

    ---Month

    ----Day

    -----PostB

     

    I just don't know how to find that PostA date when cycling throught all of this so I can't compare it to PostB's date.

     

    Any ideas? Even a starting point would be helpful.

     

    Andrew

  • webangelo 107 posts 190 karma points
    Apr 05, 2010 @ 22:40
    webangelo
    0

    Andrew,

    I believe you are asking for how to get the previous sibiling's data in a sorted list of Blog Posts.  The following discussion may have the answer you need.

    http://our.umbraco.org/forum/developers/xslt/8098-Get-property-of-sibling-within-loop

    --Chris

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 05, 2010 @ 22:55
    Chriztian Steinmeier
    0

    Hi Andrew,

    You should definitely look into using keys for this, i.e., define a key to lookup posts by their posted date:

    <xsl:key name="posts-by-date" match="node[@nodeTypeAlias = 'BlogPost']" use="substring(@postedDate, 1, 10)" />

    Now you can find all posts from any date using the key() function:

    <xsl:variable name="posts" select="key('posts-by-date', substring(@postedDate))" />

    So far, so good...

    Now would be a good time to read about 'Muenchian Grouping' - Jeni Tennison explains it very well here: http://www.jenitennison.com/xslt/grouping/muenchian.html - so you can group by date and only show the date next to the first item.

    Hope it helps, otherwise let us know,

    /Chriztian

  • Jesper Hauge 298 posts 487 karma points c-trib
    Apr 06, 2010 @ 01:04
    Jesper Hauge
    1

    This can be done without keys and Muenchian Grouping:

    <?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" 
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
    
    
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    
    <xsl:param name="currentPage"/>
    <xsl:variable name="numberOfLatestPosts" select="3" />
    <xsl:variable name="blogPostSelection">
        <posts>
            <xsl:for-each select="$currentPage/ancestor::node[@nodeName='Home']//node[@nodeTypeAlias='BlogFolder']//node[@nodeTypeAlias='BlogPost' and position() &lt; $numberOfLatestPosts]">
                <xsl:sort select="./data[@alias='publishDate']" order="descending" />
                <xsl:copy-of select="." />
            </xsl:for-each>
        </posts>
    </xsl:variable>
    <xsl:variable name="blogPosts" select="msxml:node-set($blogPostSelection)" />
    
    <xsl:template match="/">
        <table>
            <tr>
                <th>Date</th>
                <th>Header</th>
            </tr>
            <xsl:apply-templates select="$blogPosts//node" />
        </table>
    </xsl:template>
    
    <xsl:template match="node">
        <xsl:variable name="pos" select="position()" />
        <tr>
            <!-- Date -->
            <td>
                <xsl:if test="$pos = 1">
                    <xsl:value-of select="./data[@alias='publishDate']" /><br/>
                </xsl:if>
                <xsl:if test="$pos &gt; 1">
                    <xsl:variable name="prevDate" select="$blogPosts//node[number($pos) - 1]/data[@alias='publishDate']" />
                    <xsl:if test="$prevDate != ./data[@alias='publishDate']">
                        <xsl:value-of select="./data[@alias='publishDate']" />
                    </xsl:if><br/>
                </xsl:if>
            </td>
            <!-- Header -->
            <td>
                <xsl:value-of select="./data[@alias='header']" />
            </td>
        </tr>
    </xsl:template>
    
    </xsl:stylesheet>
    

    This code creates a variable containing a list of blogPost nodes from the various blogs that is sorted by a date in a set property. (You can use @createDate instead if you like, but then you'll have to do a substring comparison on the datepart of the attribute in the last if statement)

    This variable is converted into a node set with the msxml:node-set function, and this nodeset is then listed out.

    Regards
    Jesper Hauge

  • Andrew Blackmore 84 posts 127 karma points
    Apr 06, 2010 @ 15:25
    Andrew Blackmore
    0

    Hi Everyone,

     

    Thanks for all of your responses. Let me read through everything and I'll let you know what works.

Please Sign in or register to post replies

Write your reply to:

Draft