 |
| 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
|
|
|
|

August 13th, 2010, 08:08 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
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 < 10">
<xsl:value-of select ="concat('0', Schedule/AnchorDate/Year/@value,$monthDate)"/>
</xsl:variable>
<xsl:variable name ="endDate">
<xsl:value-of select ="concat(
|
|

August 13th, 2010, 08:09 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
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 < 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>
|
|

August 13th, 2010, 08:21 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Why not just do "Schedule/AnchorDate/Year/@value+1" ?
|
|

August 13th, 2010, 08:22 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

August 13th, 2010, 08:30 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
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
|
|

August 13th, 2010, 08:44 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 > 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>
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

August 13th, 2010, 08:47 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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:
|
|
|

August 13th, 2010, 08:50 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Hi Sam,
Thanks for the source code. I will try that straight away after lunch.
I really appreciate your help
|
|

August 13th, 2010, 08:51 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
|
|
Hi Michael,
Does the EXSL library works for both IE 6 + and Firefox 3 + ?
Cheers
P
|
|

August 13th, 2010, 09:32 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>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:
|
|
|
 |