Copied to clipboard

Flag this post as spam?

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


  • Matt Taylor 859 posts 2052 karma points
    Jun 20, 2010 @ 19:25
    Matt Taylor
    0

    Struggling with Variables/Parameters - Maybe

    Hello all,

    I have the following XSLT which will list out some sporting events I have created content for sorted by the sport type and date..

    <xsl:variable name="source" select="/macro/source"/>

    <xsl:template match="/">

    <!-- The fun starts here -->
    <ul class="eventslist">
    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/descendant::node[@nodeTypeAlias='Event' and string(data [@alias='umbracoNaviHide']) != '1']">
      <xsl:sort select="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName" order="ascending"/>
      <xsl:sort select="data [@alias = 'Date']" order="ascending"/> 

      <xsl:variable name="sport" select="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName"/>

      <li>Sport is <xsl:value-of select="$sport"/></li>
      <li class="event">
        <a href="{umbraco.library:NiceUrl(@id)}">
        <xsl:value-of select="@nodeName"/>
        &nbsp;
        <xsl:value-of select="umbraco.library:FormatDateTime(data[@alias='Date'],'dd/MM/yyyy')"/>
        </a>
      </li>
    </xsl:for-each>
    </ul>

    </xsl:template>

     

    My Event Document Type has an ultimate picker allowing me to choose what sport the event is for.

    The rendered output will look something like this:

     

    What I would like to do is change the output so that it does not repeat the type of sport if it is the same. Like so...

    ...but I am struggling to work out how it would be done.

    I thought I would be able to do it by assigning the sport to a variable and then only outputting the 'Sport is XXXX' line if the next record changed but I'm not sure how to do that as I am so new to XSLT.

    If there are any guru's able to help I would be most grateful.

    Regards,

    Matt

  • Dan 1250 posts 3747 karma points admin c-trib
    Jun 20, 2010 @ 20:47
    Dan
    0

    Hi Matt,

    There are neater ways to do it (try looking up the Muenchian method) but I did a similar thing to this to group articles under month headings a while back.  Hopefully you'll find this post useful: http://our.umbraco.org/forum/developers/xslt/8098-Get-property-of-sibling-within-loop.

  • Tommy Poulsen 514 posts 708 karma points
    Jun 20, 2010 @ 20:53
    Tommy Poulsen
    0

    Hi Matt, for more info and grouping example of the Munchean method that Dan mentions take a look here:
      http://blackpoint.dk/umbraco-workbench.aspx?Snippet=/umbraco-workbench/xslt/grouping--distinct-values.aspx

    It's really a powerful approach, but requires you to understand xslt keys and id's (also explained on the site)

    >Tommy

     

  • Matt Taylor 859 posts 2052 karma points
    Jun 20, 2010 @ 21:21
    Matt Taylor
    0

    Hello Dan,

    Thank you for your reply.

    I looked at your posting and followed it without any problem.

    Unfortunarley when I tried it I found the use of preceeding-sibling was no good due to the way my content is organised.

    All the event items are in separate folders to make administration easier and in most cases the preceeding-sibling is nothing.

    Events

    - 2010

    -- January

    --- 2010 World Cup Semi-Final  

    --- 2010 World Cup Final  

    -- February

    --- FA Cup Final  

    - 2009

    -- January

    --- Rugby Junior League  

    -- February

    --- Grand Master

    Is these some way I can store the previous sport value in a variable?

    Regards,

     

    Matt

  • Matt Taylor 859 posts 2052 karma points
    Jun 21, 2010 @ 00:13
    Matt Taylor
    0

    Thanks for the link Tommy.

    It's late on a Sunday night and my understanding of it is just above zero but somehow I have managed to push the right buttons and get the output I require....I think.

    This is what I've ended up with, just needs some tidying.

    <xsl:variable name="source" select="/macro/source"/>
    <xsl:key  name="sports" match="node" use="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName"/>

    <xsl:template match="/">

    <!-- The fun starts here -->
    <ul class="eventslist">
    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/descendant::node[@nodeTypeAlias='Event' and string(data [@alias='umbracoNaviHide']) != '1' and generate-id() = generate-id(key('sports', umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName)[1])]">
    <xsl:sort select="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName" />
      <li><xsl:value-of select="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName"/><br/></li>

        <xsl:for-each select="key('sports',umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName)">
        <xsl:sort select="data [@alias = 'Date']" order="ascending"/>       
        <li class="event">
            <a href="{umbraco.library:NiceUrl(@id)}">
            <xsl:value-of select="@nodeName"/>
            &nbsp;
            <xsl:value-of select="umbraco.library:FormatDateTime(data[@alias='Date'],'dd/MM/yyyy')"/>
            </a>
          </li>
          </xsl:for-each>
    </xsl:for-each>
    </ul>

    </xsl:template>

    Thanks again guys.

  • Tommy Poulsen 514 posts 708 karma points
    Jun 21, 2010 @ 08:26
    Tommy Poulsen
    0

    Hi Matt, hope you got it working - unless don't hesitate to ask

    >Tommy

  • Matt Taylor 859 posts 2052 karma points
    Jul 31, 2010 @ 00:01
    Matt Taylor
    0

    Hello all,

     

    I'm back again with a query about this Muenchian method.

    This code was all working well...

     <xsl:variable name="source" select="/macro/source"/>
    <xsl:key name="sports" match="node" use="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName"/>

    <xsl:template match="/">
    <!-- The fun starts here -->
    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/descendant::node[@nodeTypeAlias='Event' and string(data [@alias='umbracoNaviHide']) != '1' and generate-id() = generate-id(key('sports', umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName)[1])]">
    <xsl:sort select="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName" />
    <a class="menuitem submenuheader" href="#" >
    <xsl:value-of select="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName"/>
    </a>
    <div class="submenu">
    <ul>
    <xsl:for-each select="key('sports',umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName)">
    <xsl:sort select="data [@alias = 'EventCalendarDate']" order="ascending"/>
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </div>
    </xsl:for-each>
    </xsl:template>

    ...and listing all my sporting events nicely grouped by sport e.g.

    Chess
        Grand Master
    Football
        FA Cup Final
        2010 World Cup Semi-Final
        2010 World Cup Final
    Rugby
        Rugby Junior League

    However during recent testing I discovered that if I set the umbracoNaviHide = true on the 'FA Cup Final' event, all the events under the Football disappear.

    I have fixed it by moving the umbracoNaviHide!= '1' logic to an IF test later on but it feels a bit hacky.

      <xsl:variable name="source" select="/macro/source"/>
    <xsl:key name="sports" match="node" use="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName"/>

    <xsl:template match="/">
    <!-- The fun starts here -->
    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/descendant::node[@nodeTypeAlias='Event' and generate-id() = generate-id(key('sports', umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName)[1])]">
    <xsl:sort select="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName" />
    <a class="menuitem submenuheader" href="#" >
    <xsl:value-of select="umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName"/>
    </a>
    <div class="submenu">
    <ul>
    <xsl:for-each select="key('sports',umbraco.library:GetXmlNodeById(data [@alias = 'Sport'])/@nodeName)">
    <xsl:sort select="data [@alias = 'Date']" order="ascending"/>
    <xsl:if test="string(data [@alias='umbracoNaviHide']) != '1' and string(data [@alias='HideFromBySportCalendar']) != '1'">
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}" rel="eventy">
    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:if>
    </xsl:for-each>
    </ul>
    </div>
    </xsl:for-each>
    </xsl:template>

    I'm sure there's a better way to do it. Does anyone have any suggestions?

    Thanks, Matt

  • 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