Copied to clipboard

Flag this post as spam?

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


  • Chuck 71 posts 69 karma points
    Dec 12, 2011 @ 23:31
    Chuck
    0

    Need to get uniques

    I have a nodeset that I need to loop thorugh, but only display the unique values. For example, each node has a property called Title. If 5 nodes have the same exact value for the Title property, I only want to show that title one time, not 5 times. How would someone go about doing that. I don't have any code because I don't even know where to begin.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Dec 12, 2011 @ 23:51
    Chriztian Steinmeier
    0

    Hi Chuck,

    There's a method called "Muenchian Grouping" which is used to do that - it requires a leap of faith, but works pretty good:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
        <!-- Index documents by their title property - will be MUCH better if you know the Document Type, e.g.: Textpage: -->
        <!-- <xsl:key name="docByTitle" match="Textpage" use="Title" /> -->
        <xsl:key name="docByTitle" match="*[@isDoc]" use="Title" />
    
        <xsl:template match="/">
            <!-- This is the magic line that will only select the first of all unique Titled documents -->
            <xsl:for-each select="$siteRoot//*[@isDoc][count(. | key('docByTitle', Title)[1]) = 1]">
                <xsl:sort select="Title" />
    
                <xsl:apply-templates select="." mode="group" />
    
                <!-- Uncomment the following if you need to do something with all the
                    documents having the same Title as the current (unique): -->
                <!-- <xsl:apply-templates select="key('docByTitle', title)" mode="item">
                        <xsl:sort select="firstname" />
                    </xsl:apply-templates> -->
            </xsl:for-each>
        </xsl:template>
    
        <!-- Template for a uniquely Titled document -->
        <xsl:template match="*[@isDoc]" mode="group">
            <!-- TODO: Output for header -->
            <h2><xsl:value-of select="Title" /></h2>
        </xsl:template>
    
        <!-- Template for an item -->
        <xsl:template match="*[@isDoc]" mode="item">
            <!-- TODO: Output for each item -->
            <p>
                <xsl:value-of select="@nodeName" />
            </p>
        </xsl:template>
    
    </xsl:stylesheet>

    It's a start - come back with any questions you have...

    /Chriztian 

  • Chuck 71 posts 69 karma points
    Dec 12, 2011 @ 23:56
    Chuck
    0

    Ok, so if I know the docType, lets say the docType is sampleProduct. I just use that in the match field instead right?

    <xsl:keyname="docByTitle"match="sampleProduct"use="Title"/>

     

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Dec 13, 2011 @ 00:04
    Chriztian Steinmeier
    0

    Yes - and in the separate templates' match attributes as well; and after the "$siteRoot//" in the for-each: $siteRoot//sampleProduct[count( ... ) = 1]

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft