Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1461 posts 1883 karma points
    Dec 12, 2011 @ 12:15
    Gordon Saxby
    0

    Select nodes within date range

    in C# I want to select all nodes where the start and end date values cover the selected day.

    E.g. if the selected day is 2 Jan 2012 and a node has start / end dates of 1 Jan 2012 / 4 Jan 2012 then it will be selected. I tried the XSLT below which worked fine as XSLT but I cannot use it within my User Control:

    <xsl:copy-of select="$currentPage/ancestor-or-self::Calendar/CalendarAlteration [@isDoc and umbraco.library:DateGreaterThanOrEqual('2012-01-02', substring(fromDate, 1, 10)) and umbraco.library:DateGreaterThanOrEqual(substring(toDate, 1, 10), '2012-01-02')]"/>

    The date '2012-01-02' is the selected day. This gives an error "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."

    So, how can I do the equivalent of the XSLT in C# ?

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Dec 12, 2011 @ 13:24
    Lee Kelleher
    0

    Hi Gordon,

    Unfortuately you can't use custom extensions (e.g. "umbraco.library") in the XPath when calling something like GetXmlNodeByXPath.

    Not to constantly push uComponents, but if you are already using it... then you could take advantage of some uQuery methods?

    e.g.

    public List<Node> GetNodesForDate(DateTime selectedDate)
    {
        var output = new List<Node>();
        var nodes = uQuery.GetNodesByType("CalendarAlteration");
        foreach (var node in nodes)
        {
            var fromDate = node.GetPropertyAsDateTime("fromDate");
            var toDate = node.GetPropertyAsDateTime("toDate");
    
            if (selectedDate >= fromDate && selectedDate <= toDate)
            {
                output.Add(node);
            }
        }
    
        return output;
    }

    (Written off the top of my head - completely untested!)

    Cheers, Lee.

  • Gordon Saxby 1461 posts 1883 karma points
    Dec 12, 2011 @ 13:32
    Gordon Saxby
    0

    Yes, I remembered that you can't use custom extensions the first time I tried the page / code!!

    I am using uComponents so I will give your solution a go...

  • Gordon Saxby 1461 posts 1883 karma points
    Dec 13, 2011 @ 10:02
    Gordon Saxby
    1

    Thanks Lee - with a few adjustments, your suggestion worked :-) Thank you very much.

     

Please Sign in or register to post replies

Write your reply to:

Draft