Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 Display Modes
  #1 (permalink)  
Old November 25th, 2009, 12:58 AM
Registered User
 
Join Date: Nov 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Unhappy add 5 years to date in three nodes in xml document

Hi

I am trying to add 5 years to three nodes but it takes the first node date and it applies it to the following two nodes. Help would be appreciated.
Here is my xslt.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:variable name="maturDate1" select="/MxML/securities/security/financialDefinition/bondDefinition/streams/stream/maturity"/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>


<xsl:template match="/MxML/securities/security/financialDefinition/bondDefinition/streams/stream/maturity">


<xsl:variable name="maturYear" select="number(substring($maturDate1,1,4))+5"/>
<maturity>
<xsl:value-of select="concat($maturYear,substring($maturDate1,5, 4))"/>
</maturity>

</xsl:template>


</xsl:stylesheet>



INPUT XML

<MxML>
<securities>
<security>
<financialDefinition>
<bondDefinition>
<streams>
<structure>
<numberOfPhases>3</numberOfPhases>
<numberOfLegs>1</numberOfLegs>
</structure>
<stream>
<streamTemplate>
<phase>1</phase>
<leg>1</leg>
<paymentCurrency>JPY</paymentCurrency>
</streamTemplate>
<maturity>20101007 </maturity>
</stream>
<stream>
<streamTemplate>
<phase>2</phase>
<leg>1</leg>
<paymentCurrency>JPY</paymentCurrency>
</streamTemplate>
<maturity>20110414</maturity>
</stream>
<stream>
<streamTemplate>
<phase>3</phase>
<leg>1</leg>
<paymentCurrency>JPY</paymentCurrency>
</streamTemplate>
<maturity>20110414</maturity>
</stream>
</streams>
</bondDefinition>
</financialDefinition>
</security>
</securities>
</MxML>

OUTPUT

phase 2 and 3 dates are incorrect

<MxML>
<securities>
<security>
<financialDefinition>
<bondDefinition>
<streams>
<structure>
<numberOfPhases>3</numberOfPhases>
<numberOfLegs>1</numberOfLegs>
</structure>
<stream>
<streamTemplate>
<phase>1</phase>
<leg>1</leg>
<paymentCurrency>JPY</paymentCurrency>
</streamTemplate>
<maturity>20151007</maturity>
</stream>
<stream>
<streamTemplate>
<phase>2</phase>
<leg>1</leg>
<paymentCurrency>JPY</paymentCurrency>
</streamTemplate>
<maturity>20151007</maturity>
</stream>
<stream>
<streamTemplate>
<phase>3</phase>
<leg>1</leg>
<paymentCurrency>JPY</paymentCurrency>
</streamTemplate>
<maturity>20151007</maturity>
</stream>
</streams>
</bondDefinition>
</financialDefinition>
</security>
</securities>
</MxML>
Reply With Quote
  #2 (permalink)  
Old November 25th, 2009, 05:30 AM
mhkay's Avatar
Wrox Author
Points: 17,773, Level: 58
Points: 17,773, Level: 58 Points: 17,773, Level: 58 Points: 17,773, Level: 58
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,839
Thanks: 0
Thanked 267 Times in 262 Posts
Default

Your variable $maturDate1 is a node-set containing 5 element nodes.

In XSLT 1.0, when you apply the substring() function to a set of 5 nodes, it operates on the first of those nodes (2.0 would give you a type error instead).

I think you simply want:

Code:
<xsl:template match="maturity">
   			<xsl:variable name="maturYear" select="number(substring(.,1,4))+5"/> 		
   <maturity>			
     				<xsl:value-of select="concat($maturYear,substring(., 5,  4))"/>
   </maturity>
	</xsl:template>
The global variable isn't useful.

Note, your date arithmetic is capable of generating an invalid date like 20090229. Using the date/time arithmetic provided in XSLT 2.0 would be more sound.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old November 25th, 2009, 10:43 AM
Registered User
 
Join Date: Nov 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you Michael.

I have to use xslt 1.0. and my real problem that I need to update every date independently in the node-set, I have tried what you suggested to match on the maturity date. I still get the node-set and can not correctly update teh dates in each node. is there away to preserve the xml structure and add 5 years to each date in the node.

I made the global variable a local variable in the template as you suggested.

Thank you
Boris
Reply With Quote
  #4 (permalink)  
Old November 25th, 2009, 10:49 AM
mhkay's Avatar
Wrox Author
Points: 17,773, Level: 58
Points: 17,773, Level: 58 Points: 17,773, Level: 58 Points: 17,773, Level: 58
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,839
Thanks: 0
Thanked 267 Times in 262 Posts
Default

>I made the global variable a local variable in the template as you suggested.

I suggested nothing of the kind. It looks as if you didn't understand my code and changed it. Please show me the code you have now written and that still isn't working. Then perhaps I can see where your mental block is. Unfortunately writing code for people is often not a good way to solve their misunderstandings of the language.
__________________
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:
Boris1 (November 25th, 2009)
  #5 (permalink)  
Old November 25th, 2009, 11:18 AM
Registered User
 
Join Date: Nov 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Smile

Thank you very much Michael, when you said mental block, I immediately recognized what I did wrong.


Boris
Reply With Quote
Reply


Thread Tools
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
date conversion in years, months and days naveed77 Beginning VB 6 0 January 2nd, 2008 01:18 AM
adding years to date Israr JSP Basics 1 March 23rd, 2007 08:56 AM
Copying XML nodes from one document to another hughcr C# 2 May 12th, 2005 02:20 AM
Pull years from date field kev_79 Access 2 August 2nd, 2004 02:49 PM
Add and Remove Nodes in XML document. tutul128 XML 3 March 1st, 2004 10:17 AM



All times are GMT -4. The time now is 04:13 PM.


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