Copied to clipboard

Flag this post as spam?

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


  • Thomas Lee 38 posts 79 karma points
    Jul 02, 2015 @ 03:41
    Thomas Lee
    0

    ContentTypeAtXpath

    Hi Guys,

    I am trying to query xpath by specific attribute.

    var news = Umbraco.TypedContentAtXPath("//newsEventsElement[@type='News']");

    But i cant seem to get anything. Does anyone have any idea what's wrong with my query?

    Thanks in advanced!

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jul 02, 2015 @ 07:22
    Chriztian Steinmeier
    100

    Hi Thomas,

    There is no @type attribute on Document nodes.

    If newsEventsElement is the document type alias, then this is all you need:

    var news = Umbraco.TypedContentAtXPath("//newsEventsElement");
    

    On the other hand, if type is actually a property, it's stored as a child element in the XML, which would make your original XPath almost correct:

    var news = Umbraco.TypedContentAtXPath("//newsEventsElement[type='News']");
    

    Spot the difference?

    Only Umbraco's built-in properties are stored as attributes in the XML.

    Hope that helps,

    /Chriztian

  • Thomas Lee 38 posts 79 karma points
    Jul 02, 2015 @ 07:36
    Thomas Lee
    0

    Hi Chriztian,

    Thanks for the help! Its working now!

    Anyway, do you know if it's possible to do this? var news = Umbraco.TypedContentAtXPath("//newsEventsElement[date>'DateTime.Now']");

    Cheers, Thomas

  • Thomas Lee 38 posts 79 karma points
    Jul 02, 2015 @ 07:43
    Thomas Lee
    0

    Hi Chriztian, I am sorry for spamming. But is it possible to write two queries in the query?

    For example. var news = Umbraco.TypedContentAtXPath("//newsEventsElement[section='Alumni'][type='news']"); var news = Umbraco.TypedContentAtXPath("//newsEventsElement[section='Alumni' && type='news']"); var news = Umbraco.TypedContentAtXPath("//newsEventsElement[section='Alumni',type='news']");

    PS: none of this works.

    Looking forward to your reply >.<

    Thanks in advanced! Thomas

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jul 02, 2015 @ 07:43
    Chriztian Steinmeier
    0

    Hi again,

    You may be able to use the DateGreaterThanToday() extension in umbraco.library:

    var news = Umbraco.TypedContentAtXPath("//newsEventsElement[umbraco.library:DateGreaterThanToday(date)]");
    

    There's also a DateGreaterThanOrEqualToday() version, in case that's actually what you need...

    /Chriztian

  • Thomas Lee 38 posts 79 karma points
    Jul 02, 2015 @ 07:49
    Thomas Lee
    0

    Wow thanks!

    anyway i found the solution to my previous question about 2 query. Its

    var news = Umbraco.TypedContentAtXPath("//newsEventsElement[section='Alumni' and type='News']");

    I will paste it here to help others like me. ^^

    Thanks for your help Chriztian. I was stuck for hours on this.

    Cheers Thomas

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jul 02, 2015 @ 07:53
    Chriztian Steinmeier
    0

    Hi Thomas :-)

    When you add a predicate (the thing in square brackets) it filters the previous result - so when you do this:

    //newsEventsElement[section='Alumni'][type='news']
    

    You're really asking for all the elements in the Alumni section, that are of type news - everything is case-sensitive, mind you, so section and type (the property aliases) and the values Alumni and news all have to be exactly like that, casewise.

    If you want to combine results (e.g. you want all of the elements in the 'Alumni' section and all of the 'news' type elements, you can use a pipe to combine the sets:

    //newsEventsElement[section='Alumni'] | //newsEventsElement[type='news']
    

    Again, remember they're case-sensitive.

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jul 02, 2015 @ 07:55
    Chriztian Steinmeier
    0

    OK - so it was the casing of news that caught you - this would also work then:

    //newsEventsElement[section='Alumni'][type='News']
    

    /Chriztian

  • Thomas Lee 38 posts 79 karma points
    Jul 02, 2015 @ 08:00
    Thomas Lee
    0

    Hi Chriztian,

    I tried implementing,

    var news = Umbraco.TypedContentAtXPath("//newsEventsElement[umbraco.library:DateGreaterThanToday(date)]");
    

    But got this error,

    Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

    I am trying to implement this with the 'Content dropdownlist' Plugin. Do you think its possible to integrate the date comparison logic into the plugin?

    Thanks in advance! Thomas

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jul 02, 2015 @ 08:07
    Chriztian Steinmeier
    0

    Yeah I figured you'd need something like that - I'll try to summon the greater powers for you. It probably isn't that difficult to do...

    /Chriztian

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 02, 2015 @ 09:45
    Lee Kelleher
    1

    Hey... Chriztian pinged me about this, and I wasn't so sure how to do it! :-)

    So I dug into the Umbraco core code and found that it is possible to pass in variables to a query. It looks like you can only pass in string values, so using XSLT extension methods (like umbraco.library) isn't going to work.

    OK, so with a little playing around, I got the date comparison to work...

    var news = Umbraco.TypedContentAtXPath("//newsEventsElement[@isDoc and number(translate(date, '-T:', '')) < $today]", new Umbraco.Core.Xml.XPathVariable("today", DateTime.UtcNow.ToString("yyyyMMddHHmmss"))).Count()
    

    To help break it down, the translate(date, '-T:', '') bit will remove all the dashes, 'T' and colons from the datestamp, giving you a numeric value. Then with the XPathVariable, we pass in the current date/time, which we format as a numeric value too. After that the XPath expression can do the rest.


    To be completely honest, in this scenario, because we have control over the XPath expression (string), I'd be tempted to do some string formatting, rather than use an XPathVariable, e.g.

    @{
        var xpath = string.Format("//*[@isDoc and number(translate(@createDate, '-T:', '')) < '{0:yyyyMMddHHmmss}']", DateTime.UtcNow);
        var news = Umbraco.TypedContentAtXPath(xpath);
    }
    

    I hope this helps?

    Cheers,
    - Lee

  • Thomas Lee 38 posts 79 karma points
    Jul 02, 2015 @ 11:18
    Thomas Lee
    0

    Hi Lee Kelleher,

    Thanks for the time to help me with this. The code works! Dont think i can crack my head around this without you and Chriztian's help. Having some issue implementing it with the content dropdownlist plugin atm.

    Guess i will have to modify the codes accordingly to read in the date parameter.

    Once again. Thank you!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 02, 2015 @ 11:20
    Lee Kelleher
    0

    No problem Thomas, we're happy to help! :-)

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft