在XSLT 1.0中找到两个dateTime之间的MS差异

时间:2018-11-13 10:23:20

标签: xml xslt xslt-1.0

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

body, html {
  
  margin: 0;
  width: 100%;
  height: 100%;
 font-size: 16px;
  
}

* {
  
  box-sizing: border-box;
  
}

#wrap {
  
  background-color: red;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  flex-direction: column;
  
}

#box {
  
  background-color: #dcdcdc;
  padding: 10px;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  /*width: 100%; this works, but dont want it full until box is equal or smaller than content*/
  
  
}

#box > div {
  
  flex: 1 0 auto!important;
  
}

#title {
  
  text-align: center;
  margin-bottom: 20px;
  font-size: 20px;
  color: blue;
}

table {
  
  border-spacing: 0;
  border-collapse: collapse;
  table-layout: auto;
  white-space: nowrap;
  width: 100%;
}

table thead {
  
  font-weight: bold;
}

table tbody tr {
  
  height: 30px;
}

table td {
  
  padding: 0 5px;
}

#list {
  
  overflow: auto;
  background-color: rgba(0,0,0,0.05);
  
}

如何计算这两个日期之间的差(以毫秒(MS)为单位)?

我从michael.hor257k中找到了一些内容:Finding the difference between two dateTimes in XSLT 但不幸的是,我的水平太差了,无法重复使用此:(

2 个答案:

答案 0 :(得分:0)

我从工作中找到了一个好人,一个好人:

<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

答案 1 :(得分:0)

更新:

<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>