Copied to clipboard

Flag this post as spam?

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


  • Nigel Wilson 939 posts 2061 karma points
    Nov 23, 2010 @ 10:32
    Nigel Wilson
    0

    User Control xPath Nightmare

    Hi there

    I have been going round in circles trying to get this to work.

    My tree structure is:

    Home
        - Galleries
              - Competitions
                       - Competition Year
                                 - Competition
                                 - Competition
                       - Competition Year
                                 - Competition
                                 - Competition

    The competition document type has a property within it for a "close off" date.

    And so what I am wanting to do within my User Control is output a select list of all competitions that have a close off date in the future - ie competitions still open for entering.

    From sniffing around the forums, etc I have tried the following:

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(Server.MapPath(umbraco.GlobalSettings.ContentXML));
    XmlNodeList xnList = xmlDoc.SelectNodes("/root/descendant-or-self::node [@id = 1186]/competition");

            foreach (XmlNode xn in xnList)
            {
                selMembComp.Items.Add(new ListItem(xn.Attributes["id"].Value, xn.Attributes["nodeName"].Value));
            }

    The above is an initial attempt to get all competitions in the select list - after I get that working I will look to add the additional conditional checking for the date.

    Can anyone please advise where I am going wrong - I am off to bed as I have a headache (self created through frustration)!

    Thanks in anticipation

    Nigel

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Nov 23, 2010 @ 10:59
    Ismail Mayat
    0

    Nigel,

    Is there reason why you are doing this in code why not do it in xslt macro?

    Regards

    Ismail

  • Hendy Racher 861 posts 3844 karma points MVP 2x admin c-trib
    Nov 23, 2010 @ 11:29
    Hendy Racher
    0

    Hi Nigel,

    Would the method GetNodesFromXpath() in this Helper Class be of any use ? (an updated version of this class will be included in the next uComponents release)

    foreach (Node competition in UmbracoHelper.GetNodesFromXpath("/competition"))
    {
    selMembComp.Items.Add(new ListItem(competition.Name, competition.Id));
    }

    You'll just need to add the filtering to your XPath expression to get the required competition nodes.

    HTH,

    Hendy

  • Nigel Wilson 939 posts 2061 karma points
    Nov 23, 2010 @ 19:40
    Nigel Wilson
    0

    Hi Ismail and Hendy

    Thanks for your replies.

    Ismail - I am using a user control as I have an image upload component - step 1 is to select the competition, step 2 (when I get that far!) is to upload images to the chosen competition.

    Hendy - I downloaded your helper class but it didn't work :-( , looked like a nice class tho with useful methods

    So I tried the following and managed to get the select list populating as desired

    XmlNodeList xnList = xmlDoc.SelectNodes("/root/Home/galleryParent/competitionParent/competitionYear/competition");

    But being hard coded isn't a good thing so did a bit more Googling and stumbled across a forum post can't find geo datatype and Tmi Geyssens contrubutions regarding 4.5 schema made me realise how simple the required code is . . .  

    XmlNodeList xnList = xmlDoc.SelectNodes("//competition");

    This is simply selecting all competition nodes - I just have to add the logic to check for the date property . . .

    Yet again 2 lessons learnt:

    1. Umbraco rocks and more often than not the answer is blindingly simple.

    2. A good nights sleep always helps

    Regards

    Nigel

  • Nigel Wilson 939 posts 2061 karma points
    Nov 26, 2010 @ 00:40
    Nigel Wilson
    0

    Hi Hendy

    I have just revisited this and have made some progress, so will persevere with your Helper class - definitely liking the cleanliness of code.

    The following is now outputting all competition nodes in the select list (double slash is where I was going wrong previously).

    foreach (Node competition in UmbracoHelper.GetNodesFromXpath("//competition"))
    {
        selMembComp.Items.Add(new ListItem(competition.Name.ToString(), competition.Id.ToString()));
    }

    So the next step is to get the filtering working on the competition nodes based on the competition close date property.

    I tried the following but it returned no items

    DateTime todaysDate = DateTime.Now
    foreach (Node competition in UmbracoHelper.GetNodesFromXpath("//competition[/competitionCloseDate >= '" + todaysDate + "']"))
    {
        selMembComp.Items.Add(new ListItem(competition.Name.ToString(), competition.Id.ToString()));
    }

    Obviously the close off date value isn't being recognised as a date value

    Can anyone please enlighten me as to how to do a date comparison ?

    Thanks

    Nigel

     

     

  • Nigel Wilson 939 posts 2061 karma points
    Nov 26, 2010 @ 01:04
    Nigel Wilson
    0

    I have just Googled and read that date format isn't recognised and you must convert dates to numbers and compare that way.

    So I tried the following but am getting no results - can anyone advise where I am going wrong ?

    foreach (Node competition in UmbracoHelper.GetNodesFromXpath("//competition[number(concat(substring(/competitionCloseDate, 1, 4), substring(/competitionCloseDate, 6, 2), substring(/competitionCloseDate, 9, 2))) >= number(" + todaysDate + ")]"))

    I read the index starts at 1 and with the date being output as 2010-11-30T00:00:00 I assume I have the indexes correct for the substring

  • Nigel Wilson 939 posts 2061 karma points
    Nov 26, 2010 @ 02:26
    Nigel Wilson
    0

    OK - I have cracked it . . .

    foreach (Node competition in UmbracoHelper.GetNodesFromXpath("//competition[number(concat(substring(competitionCloseDate, 1, 4), substring(competitionCloseDate, 6, 2), substring(competitionCloseDate, 9, 2))) >= number(" + todaysDate + ")]"))
    {
        selMembComp.Items.Add(new ListItem(competition.Name.ToString(), competition.Id.ToString()));
    }

    So Hendy - a tick for you - your Help Class is wicked. Now to get me media issue working

     

  • 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