Copied to clipboard

Flag this post as spam?

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


  • Jamie 35 posts 87 karma points
    Aug 13, 2012 @ 12:33
    Jamie
    0

    XSLT order by dates where date field is possibly null

    I want to show the next six movies by the release date as set by a property on the movie node. Problem is some movies in the future might not have a know release date, so even though that movie certainly won't be in the next '6' the null value throws off the xslt I have. It is apparently too much to ask them to add a future estimate and come back and change it later. This is a fairly long list of movies both past and present (and future) so efficency in the select would be a concern.

    I have tried the following in my select
    umbraco.library:GetXmlNodeById($movies)/Movie [umbraco.library:DateGreaterThanOrEqualToday(date) and date = !""] but the DateGreaterThanOrEqualToday seems to be called anyway and errors the xslt.

     

    <xsl:variable name="movies" select="macro/movies"/>
     
      <xsl:template match="/">
          
        <xsl:for-each select="umbraco.library:GetXmlNodeById($movies)/Movie [umbraco.library:DateGreaterThanOrEqualToday(date)]">
          <xsl:sort select="date" data-type="text" order="ascending" />
            
          <xsl:if test="position() &lt; 7">
            <a>
              <xsl:attribute name="href">
                <xsl:value-of select="umbraco.library:NiceUrl(@id)"/>
              </xsl:attribute>
              <img width="88px" >
                <xsl:attribute name="src">
                  <xsl:text>
                    /umbraco/ImageGen.ashx?width=88&amp;image=
                  </xsl:text>
                  <xsl:value-of select="poster"/>
                </xsl:attribute>
              </img>
            </a>
          </xsl:if>

        </xsl:for-each>
        
        
      </xsl:template>

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 13, 2012 @ 12:57
    Chriztian Steinmeier
    0

    Hi Jamie,

    You're right that the extension is called for all the Movie elements - so you need to only call that extension on the ones that have a date value. Here's how I'd do it:

    <xsl:template match="/">
            <!-- Max. movies to show -->
            <xsl:variable name="max" select="6" />
            <!-- Get all movies -->
            <xsl:variable name="movies" select="umbraco.library:GetXmlNodeById($movies)/Movie" />
            <!-- Get those with a date value -->
            <xsl:variable name="moviesWithValidDate" select="$movies[normalize-space(date)]" />
            <!-- - and those without one -->
            <xsl:variable name="moviesWithNoDate" select="$movies[not(normalize-space(date))]" />
    
            <!-- Combine them, but only call the extension on the valid dates -->
            <xsl:for-each select="$moviesWithValidDate[umbraco.library:DateGreaterThanOrEqualToday(date)] | $moviesWithNoDate">
                <!-- Sort first by having a date -->
                <xsl:sort select="number(not(normalize-space(date)))" data-type="number" order="ascending" />
                <!-- - then by the date value -->
                <xsl:sort select="date" data-type="text" order="ascending" />
    
                <xsl:if test="position() &lt;= $max">
                    <div>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                        <img src="/umbraco/ImageGen.ashx?width=88&amp;image={poster}" width="88" />
                    </a>
                    </div>
                </xsl:if>
    
            </xsl:for-each>
        </xsl:template>

    /Chriztian

  • Eric Schrepel 161 posts 226 karma points
    Feb 11, 2013 @ 20:24
    Eric Schrepel
    0

    Is there a Razor equivalent to allow nulls in the OrderBy statement? Trying to do a sort of complex sort on documents where we sort on Official Date (if there is one), otherwise Umbraco's built-in CreationDate.

    Currently any nulls cause the following statement to throw an error:

    var items = Model.AncestorOrSelf().Descendants("Article").OrderBy("OfficialDate desc, CreateDate Desc")

Please Sign in or register to post replies

Write your reply to:

Draft