tags:

views:

227

answers:

3

I have a for each which loops round news item nodes. Among other properties these news items have two attributes for created date. System added date and a user entered created date (to override the system date). I would like the list sorted by created date with the preference on the user entered date.

Below is my humble invalid attempt!

<xsl:for-each select="$currentPage/ancestor-or-self::node /node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '1']">

<xsl:choose>
 <xsl:when test="data [@alias = 'createdDate'] != ''">
  <xsl:variable name="sort" select="string(data [@alias = 'createdDate'])"/>
 </xsl:when>
 <xsl:otherwise>
  <xsl:variable name="sort" select="string(@createDate)"/>
 </xsl:otherwise>
</xsl:choose>

<xsl:sort select="$sort" order="descending"/>

Many thanks

A: 

Right, seems like a hack but I have been able to achieve this by using a concat with the sort.

Example below

<xsl:for-each select="$currentPage/ancestor-or-self::node /node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '1']">
<xsl:sort select="concat(data [@alias = 'createdDate'],@createDate)" order="descending"/>
Max
A: 

To test if a node is empty (or omitted) in XSLT:

<xsl:when test="not(string(name))">...</xsl:when>
<!-- or -->
<xsl:when test="not(name)">...</xsl:when>
BalusC
+3  A: 
<xsl:sort select="(data[@alias='createdDate' and normalize-space() != '']|@createDate)[last()]" order="descending" />

This statement creates a nodeset with the two nodes containing date, and get the last one according the document order to do the sorting. If data node exists and is not empty, it will be used for the sorting because child elements of an element occur after its attribute nodes.

concat() can only work, and in a few cases, if you use text sorting; it will fail with numeric sorting.

Erlock
I thought this worked but on closer inspection it doesn't quite. If the data node with an alias of createdDate is present it does use it however if it is not present the @createDate is not used instead. Any ideas on this? Seems more robust that using concat so I would like to persevere.
Max
bit more information. I think the data node does exist but it is empty. In that case I would like to use @createDate.
Max
OK, you just didn't expose everything. ;)If empty data nodes must not be elected for sorting, predicate must be modified therefore. I edited my answer.
Erlock
that's brilliant!
Max