Script repeats results

ssargam's Avatar

ssargam

Jul 10, 2013 @ 05:46 PM

I am trying to make an archive index page that lists the titles of all the pages in same directory. I am trying to group items by month and sort by date, but the grouped values(for each month) are repeating. Any ideas on how to solve this?
All the

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="date long" extension-element-prefixes="date-converter" version="1.0" xmlns:date="http://xml.apache.org/xalan/java/java.util.Date" xmlns:date-converter="http://www.hannonhill.com/dateConverter/1.0/" xmlns:long="http://xml.apache.org/xalan/java/java.lang.Long" xmlns:xalan="http://xml.apache.org/xalan">  
    <xsl:output indent="yes" method="xml"/>

    <xsl:key match="system-folder//system-page" name="groupbymonth" use="date-converter:convertMonth(number(start-date))"/>
    <xsl:variable name="getpath" select="system-folder//system-page[name!='index']"/>




    <xsl:template match="/system-index-block">
        <xsl:apply-templates select="system-page[@current='true' and name='index']"/>
        <xsl:apply-templates select="system-folder"/>
    </xsl:template>

    
    <xsl:template match="system-page">
     
    </xsl:template>
    


    <xsl:template match="system-folder">
        <xsl:for-each select="system-page">
             <xsl:sort order="descending" select="system-page"/>
            <dl>
                <dt>
                    <xsl:choose>
                    <xsl:when test="../system-data-structure/headline != ''">
                        <xsl:value-of select="../system-data-structure/headline"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="../title"/>
                    </xsl:otherwise>
                </xsl:choose>
                </dt>
                <dd>
                    <xsl:for-each select="key('groupbymonth', date-converter:convertMonth(number(start-date)))">
                            <xsl:sort order="descending" select="start-date"/>
                        <xsl:value-of select="date-converter:convertMonth(number(start-date))"/>/<xsl:value-of select="date-converter:convertDate(number(start-date))"/>/<xsl:value-of select="date-converter:convertYear(number(start-date))"/>&#160;
                        <a href="{path}" style="text-decoration:none;">
                            <xsl:choose>
                            <xsl:when test="system-data-structure/headline != ''">
                                <xsl:value-of select="system-data-structure/headline"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="title"/>
                            </xsl:otherwise>
                        </xsl:choose>
                        </a>
                        <br/>
                        <br/>
                    </xsl:for-each>
                </dd>
            </dl>
        </xsl:for-each>


    </xsl:template>
    <!-- Xalan component for date conversion from CMS date format to RSS 2.0 pubDate format -->
    <xalan:component functions="convertDate" prefix="date-converter">
        <xalan:script lang="javascript">
            <!-- display the date and time in format "m/d/yyyy h:mm AM|PM" -->
            function convertMonth(date)
            {
                var months = new Array(13);
                months[0]  = "1";
                months[1]  = "2";
                months[2]  = "3";
                months[3]  = "4";
                months[4]  = "5";
                months[5]  = "6";
                months[6]  = "7";
                months[7]  = "8";
                months[8]  = "9";
                months[9]  = "10";
                months[10] = "11";
                months[11] = "12";
                var d = new Date(date); // Splits date into components 
                var month = months[d.getMonth()];
                return month;

            }

            function convertDate(date)
            {
                var d = new Date(date); // Splits date into components 
                var date = d.getDate();
                return date;
            }

                function convertYear(date)
            {
                var d = new Date(date); // Splits date into components 
                var year = d.getFullYear();
                return year;
            }           


        </xalan:script>
    </xalan:component>
</xsl:stylesheet>
  1. 1 Posted by Ryan Griffith on Jul 10, 2013 @ 07:26 PM

    Ryan Griffith's Avatar

    Hi Shruti,

    When you have a moment, please provide the XML output of Index Block that is being applied to this Format. This will help me test your Format locally.

    To do this, modify the following:

    <xsl:template match="/system-index-block">
            <xsl:apply-templates select="system-page[@current='true' and name='index']"/>
            <xsl:apply-templates select="system-folder"/>
    </xsl:template>
    

    To:

    <xsl:template match="/system-index-block">
            <xsl:copy-of select="." />
    </xsl:template>
    

    This should dump the Index Block's XML to your Page's source, which can be found by viewing the source code of the preview pane. Specifically, look for the XML beginning with <system-index-block> and ending with </system-index-block>.

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by ssargam on Jul 10, 2013 @ 08:42 PM

    ssargam's Avatar

    I am not sure if I understood you correctly. After replacing the part of code, I went to preview and clcked on xml to see this:

    <system-index-block current-time="1373488831256" name="breadcrumbs" type="folder"><system-folder id="c4aa6b71c0a8015600b10d845ca0a527"><name>/</name><path>//</path><site>GWC</site><link>site://GWC/</link><system-folder current="true" id="c4ad0417c0a8015600b10d84b4fd0033"><name>features</name><display-name>Features</display-name><path>/features</path><site>GWC</site><link>site://GWC/features</link><system-page current="true" id="c4bbe62ec0a8015600b10d845a20dab1"><name>index</name><title>Featured Stories</title><display-name>Featured Stories</display-name><path>/features/index</path><site>GWC</site><link>site://GWC/features/index</link><dynamic-metadata><name>left-nav</name><value>Yes</value></dynamic-metadata><dynamic-metadata><name>keywords</name></dynamic-metadata></system-page></system-folder></system-folder></system-index-block>
    

    Is this what you need?

  3. 3 Posted by Ryan Griffith on Jul 11, 2013 @ 12:25 PM

    Ryan Griffith's Avatar

    Hm, this is definitely close; however, I am not seeing any Pages other than the Features index page. The Index Block is called breadcrumbs, could this have been an output of the wrong Region/Format by chance?

    When you have a moment, would you be able to take a screenshot of the Edit screen for the Index Block you are applying to this Format? This should help me set things up locally for testing.

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by ssargam on Jul 11, 2013 @ 01:32 PM

    ssargam's Avatar

    Hi Ryan;

    Thank you so much for your help. I am attaching the screenshot of the index block, edit screen

  5. 5 Posted by ssargam on Jul 11, 2013 @ 04:47 PM

    ssargam's Avatar

    I am really confused at this point, did you happen to find something?

    Much appreciate your help.

  6. 6 Posted by Ryan Griffith on Jul 11, 2013 @ 06:14 PM

    Ryan Griffith's Avatar

    Hi Shruti,

    Although I was not yet able to group by both year and month, here is a Format that should group your Pages by year and sort them by their start-date. Perhaps this will give a decent starting point.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="date long xalan" extension-element-prefixes="date-converter" version="1.0" xmlns:date="http://xml.apache.org/xalan/java/java.util.Date" xmlns:date-converter="http://www.hannonhill.com/dateConverter/1.0/" xmlns:long="http://xml.apache.org/xalan/java/java.lang.Long" xmlns:xalan="http://xml.apache.org/xalan">  
        <xsl:output indent="yes" method="xml"/>
    
        <xsl:key match="start-date" name="groupbymonth" use="date-converter:convertMonth(number(.))"/>
        <xsl:key match="start-date" name="groupbyyear" use="date-converter:convertYear(number(.))"/>    
    
        <xsl:template match="/system-index-block">
            <xsl:for-each select="//system-page/start-date[count(. | key('groupbyyear', date-converter:convertYear(number(.)))[1]) = 1]">
                <xsl:sort order="descending" select="."/>
                <xsl:variable name="currentYear" select="date-converter:convertYear(number(.))"/>
                 
                <dl>
                    <dt><xsl:value-of select="$currentYear"/></dt>
                    <xsl:apply-templates select="../../system-page[date-converter:convertYear(number(start-date)) = $currentYear]">
                        <xsl:sort order="descending" select="start-date"/>        
                    </xsl:apply-templates>                
                </dl>
            </xsl:for-each>
        </xsl:template>
        
        <xsl:template match="system-page">
            <xsl:variable name="startDate" select="start-date"/>
            <xsl:variable name="currentDate" select="date-converter:convertDate(number($startDate))"/>
            <xsl:variable name="currentMonth" select="date-converter:convertMonth(number($startDate))"/>
            <xsl:variable name="currentYear" select="date-converter:convertYear(number($startDate))"/>
            
            <dd>
                <xsl:value-of select="$currentMonth"/>/<xsl:value-of select="$currentDate"/>/<xsl:value-of select="$currentYear"/>
                <a href="{path}" style="text-decoration:none;">
                    <xsl:choose>
                        <xsl:when test="system-data-structure/headline != ''">
                            <xsl:value-of select="system-data-structure/headline"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="title"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </a>
            </dd>
        </xsl:template>
        
        <!-- Xalan component for date conversion from CMS date format to RSS 2.0 pubDate format -->
        <xalan:component functions="convertDate" prefix="date-converter">
            <xalan:script lang="javascript">
                <!-- display the date and time in format "m/d/yyyy h:mm AM|PM" -->
                function convertMonth(date)
                {
                    var months = new Array(13);
                    months[0]  = "1";
                    months[1]  = "2";
                    months[2]  = "3";
                    months[3]  = "4";
                    months[4]  = "5";
                    months[5]  = "6";
                    months[6]  = "7";
                    months[7]  = "8";
                    months[8]  = "9";
                    months[9]  = "10";
                    months[10] = "11";
                    months[11] = "12";
                    var d = new Date(date); // Splits date into components 
                    return months[d.getMonth()];
                }
    
                function convertDate(date)
                {
                    var d = new Date(date); // Splits date into components 
                    return d.getDate();
                }
    
                function convertYear(date)
                {
                    var d = new Date(date); // Splits date into components 
                    return d.getFullYear();
                }           
    
    
            </xalan:script>
        </xalan:component>
    </xsl:stylesheet>
    

    Please let me know if you have any questions.

    Thanks!

  7. 7 Posted by ssargam on Jul 11, 2013 @ 07:42 PM

    ssargam's Avatar

    Thanks Ryan, this is is perfect.

    Thanks for your help! :)

  8. 8 Posted by Ryan Griffith on Jul 11, 2013 @ 08:07 PM

    Ryan Griffith's Avatar

    Thank you for the follow up, I am glad to hear that did the trick.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  9. Ryan Griffith closed this discussion on Jul 17, 2013 @ 07:10 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac