Wrox Programmer Forums
|
BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0
This is the forum to discuss the Wrox book XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition by Michael Kay; ISBN: 9780470192740
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 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
 
Old December 4th, 2009, 05:37 AM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default Issue with comparing dates

Hi - I have a function that I've defined in XSLT that converts a date that looks like 1/4/2010 (Jan 4, 2010) into the ISO format 20100104. This is what my function looks like:

Code:
	<xsl:function name="lbs:formatDateISO">
		<xsl:param name="varDate"/>
		<xsl:value-of select="number(concat(tokenize($varDate,'/')[3],substring('0',1,2 - string-length(tokenize($varDate,'/')[1])),tokenize($varDate,'/')[1],substring('0',1,2 - string-length(tokenize($varDate,'/')[2])),tokenize($varDate,'/')[2]))" /> 
	</xsl:function>

I then use this function in another function that filters for dates that aren't within a given startDate and endDate. I use the &gt; operator to compare dates for this and this part isn't working correctly. For example, the following code shouldn't return anything (since 1/10/2010 is "less than" 1/15/2010 and hence the test should fail) but it does return -5:

Code:
				<xsl:if test="lbs:formatDateISO('1/10/2010') &gt; lbs:formatDateISO('1/15/2010') ">
					<xsl:value-of select="lbs:formatDateISO('1/10/2010') - lbs:formatDateISO('1/15/2010')"/>
				</xsl:if>
The fact that the difference of -5 was computed correctly seems to indicate that the conversion to a number is working OK from the lbs:formatDateISO function (I confirmed this by printing out the values from this function). So I'm guessing the problem is with the greater-than operator &gt; not working as expected? Would appreciate any help. Thanks!
 
Old December 4th, 2009, 06:03 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The conversion to a number only happens when you call the '-' operator. The '>' comparison operator will work quite happily on strings, were for example '2' > '15' is true.

To ensure the returned value of the function is a number you can add the 'as' attribute to the function declaration:

<xsl:function name="lbs:formatDateISO" as="xs:integer">
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
baseliner (December 4th, 2009)
 
Old December 4th, 2009, 06:12 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Just as a quick addendum the xpath number() function returns the number in IEEE notation, which was then being converted into a string. So get rid of the number() function and add the xs:integer type to the function and it works fine.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 4th, 2009, 06:17 AM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Aah, I see. I figured that using the number() function to convert the string into a number within the function would ensure that the return value of the function would be an integer. Guess not. I used the as="xs:integer" and it gave an error that it couldn't convert 2.0100104E7 to an integer so I removed the number() function and that did the trick. Thank you!
 
Old December 4th, 2009, 06:22 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

My own approach would be: instead of converting the date to a number 20100104, convert it first to the string "2010-01-04" and then to an xs:date. While converting to a number is fine for determining which date is the earlier, converting to xs:date will also open the door to a much wider set of computations, for example working out the difference between two dates as a number of days. For the same amount of effort you have therefore gained the ability to satisfy a wider range of possible future requirements.

By the way, it is always good practice to use the "as" attribute to declare the types of function parameters and the type of the result. This makes a very big difference to the ease of debugging this kind of problem, it should really be your universal coding practice.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
baseliner (December 4th, 2009)
 
Old December 4th, 2009, 09:28 PM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Thanks Michael, that's a great idea to convert to xs:date! This also helped me compute a date that's offset from a given date by a given duration, which was my next step.. very cool! :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing dates help. warhero XSLT 1 July 5th, 2007 09:41 AM
Comparing dates Tomi XSLT 1 September 21st, 2006 04:45 AM
Comparing Dates bennybee JSP Basics 1 April 3rd, 2005 09:31 PM
Help comparing dates Dave Brown Beginning PHP 3 December 20th, 2004 04:03 PM
Comparing Dates Issue tdaustin Classic ASP Basics 2 June 2nd, 2004 06:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.