Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 4th, 2009, 05:37 AM
Authorized User
Points: 102, Level: 2
Points: 102, Level: 2 Points: 102, Level: 2 Points: 102, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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!
Reply With Quote
  #2 (permalink)  
Old December 4th, 2009, 06:03 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
baseliner (December 4th, 2009)
  #3 (permalink)  
Old December 4th, 2009, 06:12 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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?
Reply With Quote
  #4 (permalink)  
Old December 4th, 2009, 06:17 AM
Authorized User
Points: 102, Level: 2
Points: 102, Level: 2 Points: 102, Level: 2 Points: 102, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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!
Reply With Quote
  #5 (permalink)  
Old December 4th, 2009, 06:22 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
baseliner (December 4th, 2009)
  #6 (permalink)  
Old December 4th, 2009, 09:28 PM
Authorized User
Points: 102, Level: 2
Points: 102, Level: 2 Points: 102, Level: 2 Points: 102, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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! :)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:35 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.