Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old May 22nd, 2012, 05:31 AM
Registered User
 
Join Date: May 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default calculate age

Hiya,

could someone please show me how in xslt (1.0) i can calculate the persons age - in my xml structure i have defined element <dob>YYYY-MM-DD</dob>

thank you.
 
Old May 22nd, 2012, 06:10 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Consider using the date/time handling library available at www.exslt.org. Many of the functions described in this library are available as templates which you can simply include into your stylesheet.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 24th, 2012, 07:40 AM
Registered User
 
Join Date: May 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default subtracting = NaN

in the stylesheet header im calling:
xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex"

in the template i now have following parameters declared:

<xsl:param name="dobyear" select="substring((dob),1,4)" /> //this is getting the YYYY in my dob element
<xsl:param name="currentyear" select="ex:year()" /> //this gets current year

the subtraction is:

<xsl:value-of select="round($currentyear - $dobyear)" />

in my output i am getting NaN ? expecting numerical value i.e. difference between those two values. What am i doing wrong?

thank you.
 
Old May 24th, 2012, 09:05 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't know, I'm afraid. There's nothing wrong with these fragments of code, so the bug is somewhere in the code you haven't shown us.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 24th, 2012, 10:51 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Suggest you explicitly output both values immediately before the calculation to make sure they are what you expect. If that doesn't help post an example we can test for ourselves.
__________________
Joe
http://joe.fawcett.name/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate age from a varchar birthdate field nuttylife2 SQL Server 2000 15 April 18th, 2016 10:56 AM
Age Will BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 4 December 12th, 2009 02:18 PM
Age Calculation rgerald SQL Server 2000 32 October 10th, 2007 01:12 AM
age function keyvanjan ASP.NET 1.0 and 1.1 Basics 2 August 30th, 2006 08:46 AM
Vacancy Age alannoble26 Excel VBA 2 November 23rd, 2005 03:05 AM





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