Calculating dates with XSLT

Since I am posting about XSLT anyway here is another one.
In this situation I had a XML document that had a sequence of elements in which each element represented a value per hour. At the start of the sequence there was a starting date available. Here is an example xml:


	
		
			
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
			
		
	

I had to convert this XML to a target XML in which I had to group the hourly elements per day and add a date to that day. An example looks like this:



	
		06-06-2012
		06-06-2012
		52,01
		52,01
		52,01
		52,01
		52,01
		52,01
		52,01
		52,01
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		52,01
		52,01
		52,01
		52,01
	
	
		06-06-2012
		07-06-2012
		52,01
		52,01
		52,01
		52,01
		52,01
		52,01
		52,01
		52,01
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		38,64
		52,01
		52,01
		52,01
		52,01
	

First issue was to group a sequence of elements by 24. Luckily I was able to use MapForce in this which made it a little easier to solve. You can see the mapping for this here:

The XSLT for this part can be found here.
The second issue is more complex. I had to calculate the date for each group of 24 hours. THis is not a standard thing available in XSLT but luckily there is the internet. I ran into the following solution to add days to a date by using an XSLT function:



	
		
		
		
		
		
	
	
		
		
		
		
		
	

This XSLT can be simply added to MapForce so you can use this ‘function’ in your mappings. Add the XSLT as library:

Now you can simply drag and drop the ‘add-days’ function in your mapping:

The result is this mapping:

As you can see the maturity date is now calculated by adding a day to the startdate per group-by. The complete XSLT can be found here.

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in MapForce, XML/ XSD/ XSLT. Bookmark the permalink.