 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 30th, 2005, 05:44 PM
|
|
Banned
|
|
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Comparing Dates in XSLT
I've pulled the current date into an XSLT parameter, and now I'm trying to compare the date with another date. I'm somewhat of a newbie to XSLT, so please bear with me. This is what I'm trying:
Code:
<xsl:for-each select="$nr_dbdata/root/release[end_date #60;= $currentdate_short">...</xsl:for-each>
...but no results come up. I'm assuming that it's not working because both values are dates, so they can't be compared using >= and <= operators. So is it possible to compare 2 dates within XSLT 1.0 using XPath? If so, how should I go about it? Thanks for any advice.
KWilliams
|
|

November 30th, 2005, 05:55 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This has been asked quite a few times now in this forum and I needed a solution myself. Therefore I made this small, simple and not very robust class...
Code:
public class DateCompare
{
private string format;
public DateCompare(string format)
{
this.format = format;
}
public int Compare(string date)
{
int result;
DateTime[] dates;
try
{
dates = new DateTime[2];
dates[0] = DateTime.ParseExact(date, this.format, null);
dates[1] = DateTime.Today;
result = dates[0].CompareTo(dates[1]);
}
catch(Exception)
{
result = 0;
}
return result;
}
}
... which I pass to the XSLT document as an extension object. Something like this...
Code:
XsltArgumentList arguments;
Code:
try
{
arguments = new XsltArgumentList();
arguments.AddParam("today", "", DateTime.Now.ToString(date));
arguments.AddExtensionObject("urn:date", new DateCompare(date));
...
... and then I can compare the dates in the document using the class. One problem about my class is that, if it fails to compare the two dates 0 is returned. Anyways, you can use it as a start.
BTW... Take a look at this page on how to use the class in the XSLT document. There is a good example in the bottom of the page.
Hope it helps,
Jacob.
|
|

November 30th, 2005, 05:58 PM
|
|
Banned
|
|
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi jacob,
Thanks for the quick reply. Your solution uses C#, correct? I'm using VB.NET. Do you also by chance have it in that language? If not, isn't there some sort of converter out there? I've heard about something like that in the past.
KWilliams
KWilliams
|
|

November 30th, 2005, 06:02 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, it isn't that hard to convert is it? It is just another syntax and not too much code, so go for it. You can do your own implementation of a compare class, and then just use the idea for how to do it. Look at the link I posted when I edited the previous post. There you will find some VB code.
Jacob.
|
|

November 30th, 2005, 06:04 PM
|
|
Banned
|
|
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I will give it a try. Thanks again jacob:)
KWilliams
|
|

November 30th, 2005, 06:43 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
The American mm/dd/yy date format has three big disadvantages - it's liable to be misunderstood by 80% of the world's population; it's difficult to process (especially, to sort); and it's not well supported by software, especially XML software. Always convert to the ISO format yyyy-mm-dd for processing purposes, and convert back to mm/dd/yy for display to American humans, or to a less ambiguous format if there's any chance of it being seen by non-Americans.
XSLT 2.0 can compare date values directly (once they're in ISO format).
XSLT 1.0 can only compare numbers. You can convert an ISO date to a number by removing the punctuation: translate($in, '-', ''). To convert a US date to an ISO date, use a combination of substring() and concat().
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

December 1st, 2005, 11:16 AM
|
|
Banned
|
|
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
|
quote:The American mm/dd/yy date format has three big disadvantages - it's liable to be misunderstood by 80% of the world's population; it's difficult to process (especially, to sort); and it's not well supported by software, especially XML software. Always convert to the ISO format yyyy-mm-dd for processing purposes, and convert back to mm/dd/yy for display to American humans, or to a less ambiguous format if there's any chance of it being seen by non-Americans.
|
That's really good to know, and humorous. I'll make sure to keep that in mind while I'm in the process of converting data. Thanks for the info.
Quote:
quote:XSLT 2.0 can compare date values directly (once they're in ISO format).
XSLT 1.0 can only compare numbers. You can convert an ISO date to a number by removing the punctuation: translate($in, '-', ''). To convert a US date to an ISO date, use a combination of substring() and concat().
|
So I have a few questions about the possibility of converting from 1.0 to 2.0, and I'm hoping that you can help me out. They are:
1) Will my XSLT code developed using 1.0 work in the same way in 2.0, or could I run into any issues?
2) Are there any problems/bugs with 2.0?
3) What's the process of installing 2.0 on a server?
NOTE: I understand that that I can download the files to a directory on the server, and then open one of them up, but are there any other steps that we'll need to follow?
I think that's it. Thanks again for your help.
KWilliams
|
|

December 1st, 2005, 08:49 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
So I have a few questions about the possibility of converting from 1.0 to 2.0, and I'm hoping that you can help me out. They are:
1) Will my XSLT code developed using 1.0 work in the same way in 2.0, or could I run into any issues?
Everything except some rather unlikely corner cases should run the same. If you start to take advantage of XSLT 2.0's stronger type checking (which happens when you change the stylesheet to say version="2.0") you may find code that XSLT 2.0 complains about, forcing you to do an explicit data conversion where 1.0 did it implicitly. Sometimes it will find things that were actually errors all along.
2) Are there any problems/bugs with 2.0?
That's a question about implementations, not about the spec. At present there's one complete implementation, my own Saxon product, and a couple of others such as XML Spy that are getting there. Saxon releases have bugs, of course, but you have to be doing something fairly peculiar to find them.
3) What's the process of installing 2.0 on a server?
Follow the product documentation. Saxon is Java based and you'll need a Java VM.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
 |