views:

253

answers:

2

Hi! I want to find out the difference between two Dates (in terms of minutes) appearing in two different elements as data in my XML file using XSLT. I can't use functions for this, as I am using visual studio 2008, in version 1.0 XSLT functions are not supported ..

Ex- 11/12/2009 12:00 AM 12/01/2010 12:00 PM And also I have to see that date2>date1 all the time. Thanx in advance.

although I have been successful doing similar (but comparitively smaller) calculations to some extent .. (treating them as string of characters) using templates.. I could finally conclude that .. "Templates" cannot replace "functions" but the problem with my application is it can be easily interfaced with Visual studio than any other application, (well, this interface has got to do many other functions than triggering XSL transformation) so there is no question of using some other application than visual studio.

there is a website named exslt.com, I had downloaded a readymade template from there .. but couldn't make it work in my way .. so I posted this Q, not expecting some one to post complete solution but any other alternative ways if found .. like usage of some other languages (C#, javascript and etc) along with XSLT (but not beyond XSLT 1.0).. Thanks in advance .. :)

+2  A: 

If you are saying that you have data in your XML file which is in the form 11/12/2009 12:00 AM and that you want to be able to do comparisons and calculations with them. Disappointingly, I think you might be out of luck.

XSLT 2.0 and XPath 2.0 together have support for date and time using types such as xs:date and xs:time. XSLT 1.0 does not provide this support. Some XLST implementations provide additional functionality in the form of additional functions, but I cannot see any evidence that Visual Studio 2008 provides these. Furthermore, as you have noticed, if seems not to allow you to define your own functions, which might have been another way to tackle this problem.

In similar circumstances I have made progress by storing the date/time information in my XML file in a different way which can be interpreted by XSLT. If you simply want to compare date/time values to see which is earlier, storing them in a date format such as ISO 8601 (e.g. 2009-11-22 15:00) will allow you to compare them as strings. If you need to do calculations based on differences, you will probably need to use an integer-based format, such as Unix time (seconds since 1970).

Alternatively, look for other XSLT tools which support 2.0.

Tim
infant programmer
although I have been successful doing similar (but comparitively smaller) calculations to some extent .. (treating them as string of characters) using templates.. I could finally conclude that .. "Templates" cannot replace "functions" but the problem with my application is it can be easily interfaced with Visual studio than any other application, (well, this interface has got to do many other functions than triggering XSL transformation) so there is no question of using some other application than visual studio.
infant programmer
there is a website named exslt.com, I had downloaded a readymade template from there .. but couldn't make it work in my way .. so I posted this Q, not expecting some one to post complete solution but any other alternative ways if found .. like usage of some other languages (C# and etc) along with XSLT ..Thanks for responding
infant programmer
Templates can replace functions. In fact, they _are_ functions - they have arguments and return value, and they can be called recursively, which is all you need to implement any algorithm, however complex. There's also `exsl:node-set` to convert result tree fragments back to nodesets, and you're all set at this point. It can just be messy at times...
Pavel Minaev
+2  A: 

If by "Visual Studio 2008" you actually mean .NET and XslCompiledTransform, then by far the easiest way to handle this is to use its scripting extension to embed some C# code to deal with this. E.g.:

<xsl:stylesheet xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:cs="urn:cs">
     <msxsl:script language="C#" implements-prefix="cs">
     <![CDATA[
         public int MinutesBetween(string d1, string d2) {
             return (DateTime.Parse(d1) - DateTime.Parse(d2)).Minutes;
         }
     ]]>
     </msxsl:script>

     ...

     <xsl:value-of select="cs:MinutesBetween($d1, $d2)" />

</xsl:stylesheet>
Pavel Minaev
ThanQ very much Mr. Pavel
infant programmer