在XSLT1.0中查找MS中两个dateTimes之间的差异



我有一个XML文件,其中包括一些带起点和终点的日期,如下所示:

<events>
<event>
<start time="2018-10-20T10:11:36.465Z"/>
<end time="2018-10-20T09:33:04.349Z"/>
</event>
</events>

如何计算这两个日期之间的差异(毫秒(?

我在michael.hor257k中找到了一些东西:在XSLT中查找两个dateTimes之间的差异但不幸的是,我的水平太差,无法重复使用这个:(

我从工作中找到了一个好人的解决方案:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="event">
<xsl:variable name="start">
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="start/@time" />
</xsl:call-template>
</xsl:variable> 
<xsl:variable name="end">
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="end/@time" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="duration" select="$start - $end" />

<duration>
<xsl:value-of select="$duration"/>
</duration>
</xsl:template>
<xsl:template name="dateTime-to-seconds">
<xsl:param name="dateTime"/>
<xsl:variable name="datetmp" select="substring-before($dateTime, 'Z')" />
<xsl:variable name="date" select="substring-before($datetmp, 'T')" />
<xsl:variable name="time" select="substring-after($datetmp, 'T')" />
<xsl:variable name="local-time" select="substring($time, 1, string-length($time) - 4)" />
<xsl:variable name="year" select="substring($date, 1, 4)" />
<xsl:variable name="month" select="substring($date, 6, 2)" />
<xsl:variable name="day" select="substring($date, 9, 2)" />
<xsl:variable name="hour" select="substring($local-time, 1, 2)" />
<xsl:variable name="minute" select="substring($local-time, 4, 2)" />
<xsl:variable name="second" select="substring($local-time, 7)" />
<xsl:variable name="millis" select="substring-after(substring-after($time, $local-time),'.')" />
<xsl:variable name="yearms" select="$year * 31556900000" />
<xsl:variable name="months" select="$month * 2629740000" />
<xsl:variable name="dayms" select="$day * 86400000" />
<xsl:variable name="hrms" select="$hour * 3600000" />
<xsl:variable name="minms" select="$minute * 60000" />
<xsl:variable name="secms" select="$second * 1000" />

<xsl:value-of select="$yearms + $months + $dayms + $hrms + $minms + $secms + $millis " />
</xsl:template> 
</xsl:stylesheet>

演示:http://xsltransform.net/jxWYjVm/2

更新:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="event">
<xsl:variable name="start">
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="start/@time" />
</xsl:call-template>
</xsl:variable> 
<xsl:variable name="end">
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="end/@time" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="duration" select="$end - $start" />
<duration>
<xsl:value-of select="$duration"/>
</duration>
</xsl:template>

<xsl:template name="dateTime-to-seconds">
<xsl:param name="dateTime"/>
<xsl:variable name="datetmp" select="substring-before($dateTime, 'Z')" />
<xsl:variable name="date" select="substring-before($datetmp, 'T')" />
<xsl:variable name="time" select="substring-after($datetmp, 'T')" />
<xsl:variable name="local-time" select="substring($time, 1, string-length($time) - 4)" />
<xsl:variable name="year" select="substring($date, 1, 4)" />
<xsl:variable name="month" select="substring($date, 6, 2)" />
<xsl:variable name="day" select="substring($date, 9, 2)" />
<xsl:variable name="hour" select="substring($local-time, 1, 2)" />
<xsl:variable name="minute" select="substring($local-time, 4, 2)" />
<xsl:variable name="second" select="substring($local-time, 7)" />
<xsl:variable name="millis" select="substring-after(substring-after($time, $local-time),'.')" />
<xsl:variable name="a" select="floor((14 - $month) div 12)"/>
<xsl:variable name="y" select="$year + 4800 - $a"/>
<xsl:variable name="m" select="$month + 12*$a - 3"/>    
<xsl:variable name="jd" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
<xsl:value-of select="((86400*$jd + 3600*$hour + 60*$minute + $second) * 1000) + $millis " />
</xsl:template> 

</xsl:stylesheet>

相关内容

  • 没有找到相关文章

最新更新