Copied to clipboard

Flag this post as spam?

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


  • bayshield 50 posts 65 karma points
    Jul 20, 2010 @ 16:20
    bayshield
    0

    List documents grouped by first letter

    I  have around 300 documents in my content tree which I would like to group by first letter i.e.

    A

    Adams, Alister

    B

    Barry, Barnes,Belfry

    C

    Chad, Chester

    etc...

    I have  written some xslt which takes a letter as a parameter and will output the docs for that letter, however I need to call it 26 times (once for each letter) which doesn't sit well with me!

    Are there any XSLT gurus out there who can shed some light on a best practice way of doing this?

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Jul 20, 2010 @ 16:38
    Thomas Höhler
    0

    There are some grouping samples and threads available, see http://our.umbraco.org/forum/developers/xslt/2988-Help-grouping-and-sorting-by-category

    Or take a look into the grouping example here: http://blackpoint.dk/umbraco-workbench/xslt/grouping--distinct-values.aspx?p=3

    hth, Thomas

     

  • Sean Mooney 131 posts 158 karma points c-trib
    Jul 20, 2010 @ 16:40
    Sean Mooney
    1

    Here is an example I use for a glossary listing

    <xsl:for-each select="$currentPage/node">
    <xsl:sort select="@nodeName" />
        <xsl:variable name="Init" select="substring(./@nodeName,1,1)"/>
        <xsl:if test="not(preceding-sibling::*[substring(./@nodeName,1,1)=$Init])">
            <div id="{$Init}" class="terms">
                <h3><xsl:value-of select="$Init"/></h3>
                <dl>
                <xsl:for-each select="../node[substring(./@nodeName,1,1)=$Init]">
                    <dt id="{@urlName}"><xsl:value-of select="@nodeName"/></dt>
                    <dd><xsl:value-of select="./data [@alias='definition']" disable-output-escaping="yes" /></dd>
                </xsl:for-each>
                </dl>
                <a href="javascript:window.scrollTo(0,0)">Back to top</a>
            </div>
        </xsl:if>
    </xsl:for-each>

    It basically takes the first letter of the node name and uses that to find all siblings that start with the same

  • dandrayne 1138 posts 2262 karma points
    Jul 20, 2010 @ 17:01
    dandrayne
    1

    You could also organise the docs in "alphabet folders" in the backend for the benefit of admins using something like http://our.umbraco.org/projects/developer-tools/autofolders, then the xslt becomes very simple as a side-result

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jul 20, 2010 @ 18:20
    Chriztian Steinmeier
    1

    Hi bayshield,

    If you already have the code for finding and writing, then you can use this setup to do the alphabet-walk:

       <xsl:variable name="letters" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
    
        <xsl:template match="/">
            <ul>
                <xsl:call-template name="writeIndex" />
            </ul>
        </xsl:template>
    
        <xsl:template name="writeIndex">
            <xsl:param name="index" select="1" />
            <xsl:call-template name="writeNodesForLetter">
                <xsl:with-param name="letter" select="substring($letters, $index, 1)" />
            </xsl:call-template>
            <xsl:if test="not($index = string-length($letters))">
                <xsl:call-template name="writeIndex">
                    <xsl:with-param name="index" select="$index + 1" />
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    
        <!-- Template for nodes replace with own code -->
        <xsl:template name="writeNodesForLetter">
            <xsl:param name="letter" />
            <li>
                <xsl:value-of select="$letter" />
            </li>
        </xsl:template>
    

    /Chriztian

  • bayshield 50 posts 65 karma points
    Jul 28, 2010 @ 11:46
    bayshield
    0

    Thanks for the input guys!  Sean's XSLT worked a treat!

  • David 17 posts 40 karma points
    Sep 06, 2010 @ 13:17
    David
    0

    Hey just thought I'd add a little appreciation for Seans code too. Just adapted it for a similar purpose (although I'm using all tags in a group as my list source) and it works a treat. Wicked! Thanks.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft