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 August 13th, 2010, 08:08 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default How to add 12 moths to a date

Hi,

1 - Please could someone shed some light on how to add 12 months (1 year) to a start date?

In the xml below I have an AnchorDate element that contains Year and Month elements.

They form the start date like this - 201008 (YYYYMM)

Based on these values I need to get the end date that will be 1 year later.

2 - Also, is there a better way to concat a 0 (zero) in front of the month if the month is less than 10 or my approach is ok?

Cheers

P

<Schedule>
<AnchorDate>
<Month value="8"></Month>
<Year value="2010"></Year>
</AnchorDate>
</Schedule>

<xsl:variable name ="monthDate">
<xsl:if test ="Schedule/AnchorDate/Month/@value &lt; 10">
<xsl:value-of select ="concat('0', Schedule/AnchorDate/Year/@value,$monthDate)"/>
</xsl:variable>



<xsl:variable name ="endDate">
<xsl:value-of select ="concat(
 
Old August 13th, 2010, 08:09 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi,

For some reason my text is not formatting well. I will post the code below again.


<Schedule>
<
AnchorDate>
<
Monthvalue="8"></Month>
<
Yearvalue="2010"></Year>
</
AnchorDate>
</
Schedule>

<
xsl:variablename ="monthDate">
<
xsl:iftest ="Schedule/AnchorDate/Month/@value &lt; 10">
<
xsl:value-ofselect ="concat('0', Schedule/AnchorDate/Month/@value)"/>
</
xsl:if>
</
xsl:variable>

<
xsl:variablename ="startDate">
<
xsl:value-ofselect ="concat(Schedule/AnchorDate/Year/@value,$monthDate)"/>
</
xsl:variable>

<
xsl:variablename ="endDate">
<
xsl:value-ofselect ="concat(Schedule/AnchorDate/Year/@value,$monthDate)"/>
</
xsl:variable>

 
Old August 13th, 2010, 08:21 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Why not just do "Schedule/AnchorDate/Year/@value+1" ?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 13th, 2010, 08:22 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The answers to both questions depend on whether you are using XSLT 1.0 or 2.0.

However, since your year and month are in separate elements, I would have thought you could simply add 1 to the year.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old August 13th, 2010, 08:30 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Michael and Sam,

Thanks for your reply.

Sorry, I forgot to mention that I am using XSLT 1 and MSXML.

1 - But what about the month?

If my start date is 201008 then my end date has to be 201107

2 - Also what happans when my start date is 201001?

In this case I would not have to add 1 to year and the end date would be:

201012

Cheers

P
 
Old August 13th, 2010, 08:44 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

That isn't adding 12 months - that's adding 11.

Code:
<xsl:variable name="month" select="Schedule/AnchorDate/Month/@value"/>
<xsl:variable name="year" select="Schedule/AnchorDate/Year/@value"/>

<xsl:variable name="newYear">
<xsl:choose><xsl:when test="$month = 1"><xsl:value-of select="$year"/></xsl:when>
<xsl:otherwise><xsl:value-of select="$year+1"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>


<xsl:variable name="newMonth">
<xsl:choose>
<xsl:when test="$month = 1"><xsl:value-of select="'12'"/></xsl:when>
<xsl:when test="$month &gt; 10"><xsl:value-of select="$month-1"/></xsl:when>
<xsl:otherwise><xsl:value-of select="concat('0',$month-1)"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:variable name="endDate">
<xsl:value-of select="$newYear"/><xsl:value-of select="$newMonth"/>
</xsl:variable>
__________________
/- 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:
pallone (August 13th, 2010)
 
Old August 13th, 2010, 08:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, you said you wanted to add 12 months. Adding 11 months is more difficult. Take a look at the templates in the EXSLT library for date and time arithmetic, which handle a lot of this for XSLT 1.0. (www.exslt.org)
__________________
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:
pallone (August 13th, 2010)
 
Old August 13th, 2010, 08:50 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Smile

Hi Sam,

Thanks for the source code. I will try that straight away after lunch.

I really appreciate your help
 
Old August 13th, 2010, 08:51 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Does the EXSL library works for both IE 6 + and Firefox 3 + ?

Cheers

P
 
Old August 13th, 2010, 09:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Does the EXSL library works for both IE 6 + and Firefox 3 + ?

Some EXSLT functions are implemented as native code for particular XSLT processors, and some are implemented as generic XSLT 1.0 source code templates that can be incorporated into any stylesheet using xsl:import or xsl:include. You'll have to check the details yourself.
__________________
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:
pallone (August 13th, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add Weeks to a date? chobo2 ASP.NET 3.5 Basics 2 March 20th, 2009 07:01 PM
Add Date to File Using Cmd ecom Other Programming Languages 1 November 13th, 2007 05:16 PM
want to convert date from 01/12/07 to 12-Jan-2007 hurperl Perl 1 April 12th, 2007 05:47 AM
12/30/1899 ASP Date from SQL chris42480 Classic ASP Databases 2 March 8th, 2004 02:23 PM





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