Copied to clipboard

Flag this post as spam?

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


  • Lorenzo 5 posts 25 karma points
    Mar 15, 2011 @ 18:13
    Lorenzo
    0

    sorting xsl:for-each on different fields

    Hi,

    my "News Item" page has the default @createDate and a custom date (customCreateDate).

    In my News page I've a macro that renders the list of News

      <xsl:param name="currentPage"/>
      <xsl:param name="maxNewsItems" select="/macro/MaxNewsItems" />
      <xsl:variable name="newsItemType" select="string('NewsItem')" />
    
      <xsl:template match="/">
    
        <xsl:variable name="list" select="$currentPage/* [name() = $newsItemType]" />
        <xsl:variable name="listCount" select="count($list)" />
        <xsl:for-each select="$list">
          <xsl:sort select="@createDate" order="descending"/>
          <xsl:value-of select="summary" />
        </xsl:for-each>
      </xsl:template>
    

    At the moment I'm using the  <xsl:sort select="@createDate" order="descending"/> in order to have the lastest added first. Now I would like to sort the list for the customCreateDate setting this value the .net DateTime.Now if empty. So I would like to write something like:

       <xsl:for-each select="$list">
        <xsl:variable name="order">
    <xsl:if test="string(customCreateDate = '')"><xsl:value-of select="@CreateDate" /></xsl:if>
    <xsl:if test="string(customCreateDate != '')"><xsl:value-of select="customCreateDate" /></xsl:if>
    </xsl:variable>
          <xsl:sort select="$order" order="descending"/>
             <xsl:value-of select="summary" />
        </xsl:for-each>
      </xsl:template>

     

    any help?

     

    Thanks.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Mar 15, 2011 @ 22:57
    Chriztian Steinmeier
    2

    Hi Lorenzo,

    You should be able to do this by supplying two mutually exclusive sort statements:

    <xsl:sort select="customCreateDate[normalize-space()]" order="descending" />
    <xsl:sort select="@createDate[not(normalize-space(../customCreateDate))]" order="descending" />
    

    /Chriztian

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 15, 2011 @ 23:00
    Jan Skovgaard
    0

    Hi Chriztian

    I was wondering very long about this...this is dead simple. Brilliant! So let's see if I understand it correctly...

    I suppose it works like a <xsl:choose> where the first condition that is true skips the rest (roughly speaking), right?

    /Jan

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Mar 15, 2011 @ 23:21
    Chriztian Steinmeier
    2

    Hi Jan,

    Think of how the processor collects all the values to sort - and then take each node and examine what's selected...

    The first sort selects the <customCreateDate> if it has a value.

    The second sort selects the createDate attribute if its parent (the element it sits on) doesn't have a <customCreateDate> with a value.

    So for every node in the selection that's to be sorted, the processor will pick EITHER customCreateDate OR @createDate and sort by that value... as long as they're in the same format it will work.

    So yes, it works as if you were allowed to do a normal choose/when/otherwise to select the value for the sort statement's select attribute .

    /Chriztian

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 15, 2011 @ 23:27
    Jan Skovgaard
    0

    Hi Chriztian

    Cool - Thanks for the thourough explanation. Got a feeling this will ease things for many more out there. Have come accross this issue earlier ending up solving it in much more cumbersome ways.

    This definately goes in the "bookmarks" :-)

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft