Copied to clipboard

Flag this post as spam?

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


  • John C Scott 457 posts 1157 karma points
    Oct 27, 2011 @ 01:20
    John C Scott
    0

    yet another xsl:key question

    been looking at this all day and i mean *all* day

    not getting it sorry

    read quite a few posts about it but the syntax is beating me

    it's quite simple what i want to do using the muenchian thingy

    i have a list of documents, and i want to group them by year, they have a date picker property

    the XSLT I have looks a bit like this:

    <xsl:param name="currentPage"/>
    <xsl:key name="years" match="documentDate" use="substring(.,1,4)" />
        
    <xsl:template match="/">
      <xsl:for-each select="$currentPage/Document/documentDate [
        generate-id() = generate-id(key('years', substring(.,1,4))[1])
      ]">
        <h1><xsl:value-of select="../@nodeName"/></h1>
        <xsl:for-each select="key('years',substring(.,1,4))">
          <h2>
            <xsl:value-of select="."/>
          </h2>
          <p>
            <xsl:value-of select="substring(.,1,4)"/>
          </p>
        </xsl:for-each>
    </xsl:for-each>
    </xsl:template>

    something is not right here, presently i just get the first result only and nothing else

    all suggestions very gratefully received

    J

     

     

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Oct 27, 2011 @ 01:40
    Chriztian Steinmeier
    0

    Hi John,

    Instead of selecting the years, go for the actual Documents - here's a simple setup with a template for the "group" wrapper (e.g. where you print the year) and another for the Documents:

    <xsl:key name="Docs-by-Year" match="Document" use="substring(documentDate, 1, 4)" />
    
    <xsl:template match="/">
        <xsl:for-each select="Document[count(. | key('Docs-by-Year', substring(documentDate, 1, 4))[1]) = 1]">
            <xsl:sort select="documentDate" order="descending" />
    
            <xsl:apply-templates select="." mode="group" />
    
            <xsl:apply-templates select="key('Docs-by-Year', substring(documentDate, 1, 4))" mode="item">
                <xsl:sort select="documentDate" order="descending" />
            </xsl:apply-templates>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="Document" mode="group">
        <h2><xsl:value-of select="substring(documentDate, 1, 4)" /></h2>
    </xsl:template>
    
    <xsl:template match="Document" mode="item">
        <p>
            <xsl:value-of select="@nodeName" />
        </p>
    </xsl:template>

    /Chriztian 

  • John C Scott 457 posts 1157 karma points
    Oct 27, 2011 @ 01:48
    John C Scott
    0

    oh brilliant thank you

    i'll give this a try now 

    how are you so on it :) ?

  • John C Scott 457 posts 1157 karma points
    Oct 27, 2011 @ 02:00
    John C Scott
    0

    that's such a nicely written piece of xslt, a joy to read 

    sadly tho i still only get the top result

    not the first node but the first by date value of documentDate

    the first thought I had was that I hadn't done a republish for a while, and in a way I am relieved it wasn't that :)

    made one very small change to the script above, presuming this is ok to say

    <xsl:for-eachselect="$currentPage/Document[count(. | key('Docs-by-Year', substring(documentDate, 1, 4))[1]) = 1]">

     

  • John C Scott 457 posts 1157 karma points
    Oct 27, 2011 @ 02:06
    John C Scott
    0

    I'm struggling to understand the point of the 

    [count(. | key('Docs-by-Year', substring(documentDate, 1, 4))[1]) = 1]

    bit.

    why test the count = 1? 

     

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Oct 27, 2011 @ 08:49
    Chriztian Steinmeier
    0

    Hi John,

    Yeah the $currentPage missing as my way of checking you :-) (Not... sorry 'bout that.)

    So reasons the above wouldn't work/only return 1 result could be that there's maybe only a single Document node below $currentPage (fix by adding the double slash: $currentPage//Document[etc...]

    About the count() thing -- I wrote an elaborate explanation here about that. It's part of an evil trick to find the distinct values for a given key.

    Let me know if it still doesn't work...

    /Chriztian

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies