Copied to clipboard

Flag this post as spam?

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


  • Kim Hansen 63 posts 144 karma points
    Jan 22, 2012 @ 01:41
    Kim Hansen
    0

    Select nodes between two dates

    Hi guys

    I need some help from you to make a xslt statement.

    I need to loop through a tree containing news items, and i want to get the news items between two create dates.

    Can anybody please help me with that?

    <xsl:call-template name="search">
      <xsl:with-param name="items" select="$newsList/descendant::*[xxxxxxxxxxxxxx]"/>
    </xsl:call-template>

    Thanks

    Kim

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jan 22, 2012 @ 02:15
    Chriztian Steinmeier
    0

    Hi Kim,

    Because everything's effectively a string in XSLT, you'll need to use some of the Date* -extensions from umbraco.library, e.g.:

    <xsl:call-template name="search">
      <xsl:with-param name="items" select="$newsList//*[umbraco.library:DateGreaterThanOrEqualToday(@createDate)]"/>
    </xsl:call-template>

    Check them out, there's quite a few, so there's probably one you can use.

    /Chriztian

  • Rodion Novoselov 694 posts 859 karma points
    Jan 22, 2012 @ 16:46
    Rodion Novoselov
    0

    AFAIK, actually Umbraco stores all dates inside XML in sortable date format, so you don't have to bother about date-string conversion for comparing.

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jan 22, 2012 @ 20:06
    Chriztian Steinmeier
    0

    @Rodion: They're sortable yes, but unfortunately in XSLT 1.0, you can't compare them to see if a date is between two values. The < and > operators are simply not defined for strings...

    /Chriztian

  • Kim Hansen 63 posts 144 karma points
    Jan 22, 2012 @ 20:12
    Kim Hansen
    0

    Hi

    Maybe i didnt explain it right.

    Ive got a news search page, where i have two date selector fields, on my result page, i want the news items that is between these two dates.

    As far as i know, i cant use DateGreaterThanOrEqualToday(@createDate) as it gives me a true/false on a given date.

    What i need is something like @createDate > $inputDateStart and @createDate < $inputDateEnd

    Thanks

    Kim

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jan 22, 2012 @ 20:23
    Chriztian Steinmeier
    0

    Hi Kim,

    You should be able to do that in a single XPath selection:

    <xsl:variable name="newsItems" select="$newsList//*[umbraco.library:DateGreaterThan(@createDate, $inputDateStart) and umbraco.library:DateGreaterThan($inputDateEnd, @createDate)]" />

    /Chriztian

  • Kim Hansen 63 posts 144 karma points
    Jan 22, 2012 @ 20:58
    Kim Hansen
    0

    Thanks, but im getting an error.

    String was not recognized as a valid DateTime.


    <xsl:variablename="inputDateStart">2010-11-21T01:00:00</xsl:variable>

    <xsl:variablename="inputDateEnd">2012-11-21T01:00:00</xsl:variable>

    <xsl:copy-ofselect="$newsList//*[umbraco.library:DateGreaterThan(@createDate, $inputDateStart) and umbraco.library:DateGreaterThan($inputDateEnd, @createDate)]" />

     

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jan 22, 2012 @ 21:07
    Chriztian Steinmeier
    0

    Hi Kim,

    My bad - we need to slap the @isDoc filter on there first, to make sure it's not trying to find @createDate on the various property nodes;

    <xsl:variable name="newsItems" select="$newsList//*[@isDoc][umbraco.library:DateGreaterThan(@createDate, $inputDateStart) and umbraco.library:DateGreaterThan($inputDateEnd, @createDate)]" />

    /Chriztian

  • Kim Hansen 63 posts 144 karma points
    Jan 22, 2012 @ 21:26
    Kim Hansen
    0

    It works perfectly with a few tweaks for my nodetree.

    Thank you so much :)

    $newsList//NewsItem[@isDoc][umbraco.library:DateGreaterThan(@createDate, $inputDateStart) and umbraco.library:DateGreaterThan($inputDateEnd, @createDate)]

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jan 22, 2012 @ 21:35
    Chriztian Steinmeier
    0

    Cool Kim,

    Just want to clarify that using the alias of the Document Type ("NewsItem" in this case), you actually don't need the @isDoc filter any longer, because they do the same thing in this context - but using the alias is the best solution and will have the processor looking through the least amount of nodes for matches.

    /Chriztian

  • Rodion Novoselov 694 posts 859 karma points
    Jan 22, 2012 @ 21:45
    Rodion Novoselov
    0

    Uhmm... My fault. I looked into the spec and understood that I said complete baloney. In fact XPath doesn't know anything about datetimes and treats them as strings, meanwile relatoinal expressions besides "=" and "!=" aren't defined for strings at all (the latter is the very one that I was not aware of).

  • 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