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']");
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:
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?
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);
}
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.
ContentTypeAtXpath
Hi Guys,
I am trying to query xpath by specific attribute.
But i cant seem to get anything. Does anyone have any idea what's wrong with my query?
Thanks in advanced!
Hi Thomas,
There is no
@type
attribute on Document nodes.If newsEventsElement is the document type alias, then this is all you need:
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:
Spot the difference?
Only Umbraco's built-in properties are stored as attributes in the XML.
Hope that helps,
/Chriztian
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
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
Hi again,
You may be able to use the
DateGreaterThanToday()
extension in umbraco.library:There's also a
DateGreaterThanOrEqualToday()
version, in case that's actually what you need.../Chriztian
Wow thanks!
anyway i found the solution to my previous question about 2 query. Its
I will paste it here to help others like me. ^^
Thanks for your help Chriztian. I was stuck for hours on this.
Cheers Thomas
Hi Thomas :-)
When you add a predicate (the thing in square brackets) it filters the previous result - so when you do this:
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:
Again, remember they're case-sensitive.
/Chriztian
OK - so it was the casing of news that caught you - this would also work then:
/Chriztian
Hi Chriztian,
I tried implementing,
But got this error,
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
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
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 (likeumbraco.library
) isn't going to work.OK, so with a little playing around, I got the date comparison to work...
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.I hope this helps?
Cheers,
- Lee
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!
No problem Thomas, we're happy to help! :-)
Cheers,
- Lee
is working on a reply...