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.
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() <= $max">
<div>
<a href="{umbraco.library:NiceUrl(@id)}">
<img src="/umbraco/ImageGen.ashx?width=88&image={poster}" width="88" />
</a>
</div>
</xsl:if>
</xsl:for-each>
</xsl:template>
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")
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() < 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&image=
</xsl:text>
<xsl:value-of select="poster"/>
</xsl:attribute>
</img>
</a>
</xsl:if>
</xsl:for-each>
</xsl:template>
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:
/Chriztian
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")
is working on a reply...