DateTime to TimeStamp in XSLT

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I have found a way to convert a date to unix timestamp using msxsl:script (code below is working fine).
Is there another way without using msxsl:script and XsltCompiledTransform ?
XML :

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; "<span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; root<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; date<span style="color:Blue; >2011-05-09T15:56:00<span style="color:Blue; </<span style="color:#A31515; date<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; root<span style="color:Blue; >

[/code]
XSL :


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; " <span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; xsl<span style="color:Blue; :<span style="color:#A31515; stylesheet <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; "
<span style="color:Red; xmlns:xsl<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.w3.org/1999/XSL/Transform<span style="color:Black; "
<span style="color:Red; xmlns:msxsl<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; urn:schemas-microsoft-com:xslt<span style="color:Black; "
<span style="color:Red; xmlns:timestamp<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; urn:mynamspace<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xsl<span style="color:Blue; :<span style="color:#A31515; output <span style="color:Red; method<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xml<span style="color:Black; " <span style="color:Red; indent<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; yes<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; "<span style="color:Blue; />

<span style="color:Blue; <<span style="color:#A31515; msxsl<span style="color:Blue; :<span style="color:#A31515; script <span style="color:Red; language<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; C#<span style="color:Black; " <span style="color:Red; implements-prefix<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; timestamp<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <![CDATA[<span style="color:Gray;
public int DateToTimestamp(DateTime date)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = date - origin;
int timestamp = (int)Math.Floor(diff.TotalSeconds);
return timestamp;
}

public DateTime TimestampToDate(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
DateTime date = origin.AddSeconds(timestamp);
return date;
}
<span style="color:Blue; ]]>
<span style="color:Blue; </<span style="color:#A31515; msxsl<span style="color:Blue; :<span style="color:#A31515; script<span style="color:Blue; >

<span style="color:Blue; <<span style="color:#A31515; xsl<span style="color:Blue; :<span style="color:#A31515; template <span style="color:Red; match<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; /<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xsl<span style="color:Blue; :<span style="color:#A31515; variable <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; timestamp<span style="color:Black; " <span style="color:Red; select<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; timestamp:DateToTimestamp(root/date)<span style="color:Black; "<span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xsl<span style="color:Blue; :<span style="color:#A31515; variable <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; date<span style="color:Black; " <span style="color:Red; select<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; timestamp:TimestampToDate($timestamp)<span style="color:Black; "<span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; root<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; timestamp<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xsl<span style="color:Blue; :<span style="color:#A31515; value-of <span style="color:Red; select<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; $timestamp<span style="color:Black; "<span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; timestamp<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; date<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xsl<span style="color:Blue; :<span style="color:#A31515; value-of <span style="color:Red; select<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; $date<span style="color:Black; "<span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; date<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; root<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xsl<span style="color:Blue; :<span style="color:#A31515; template<span style="color:Blue; >

<span style="color:Blue; </<span style="color:#A31515; xsl<span style="color:Blue; :<span style="color:#A31515; stylesheet<span style="color:Blue; >
[/code]
OUTPUT :


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; "<span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; root <span style="color:Red; xmlns:msxsl<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; urn:schemas-microsoft-com:xslt<span style="color:Black; " <span style="color:Red; xmlns:timestamp<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; urn:mynamspace<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; timestamp<span style="color:Blue; >1304956560<span style="color:Blue; </<span style="color:#A31515; timestamp<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; date<span style="color:Blue; >2011-05-09T15:56:00<span style="color:Blue; </<span style="color:#A31515; date<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; root<span style="color:Blue; >
[/code]

Kind regards,
<hr class="sig http://aelassas.free.fr aelassas.free.fr

View the full article
 
Back
Top