在 XSLT 中查找两个日期时间之间的差异 [英] Finding the difference between two dateTimes in XSLT

查看:27
本文介绍了在 XSLT 中查找两个日期时间之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

..........<结束时间="2016-02-22T02:24:38+02:00"/>

问题:

如何计算两个时间属性的差异?

解决方案

我收到错误:... xsl:version: only 1.0 features are supported

这是一个纯粹的 XSLT 1.0 解决方案:

<xsl:output method="xml"版本=1.0"编码=UTF-8"缩进=是"/><xsl:strip-space elements="*"/><xsl:模板匹配=事件"><xsl:变量名=开始"><xsl:call-template name="dateTime-to-seconds"><xsl:with-param name="dateTime";选择=开始/@时间";/></xsl:call-template></xsl:变量><xsl:variable name="end"><xsl:call-template name="dateTime-to-seconds"><xsl:with-param name="dateTime";选择=结束/@时间";/></xsl:call-template></xsl:变量><xsl:变量名=持续时间"select="$end - $start";/><xsl:变量名=d";select="floor($duration div 86400)"/><xsl:变量名=t";选择=$duration mod 86400"/><xsl:变量名=h";选择=地板($ t div 3600)"/><xsl:变量名=r"选择=$ t mod 3600"/><xsl:变量名=m";select="floor($r div 60)"/><xsl:变量名=s";选择=$ r mod 60"/><xsl:copy><xsl:copy-of select="name"/><持续时间><xsl:value-of select="$d"/><xsl:text>天,</xsl:text><xsl:value-of select="$h"/><xsl:text>小时,</xsl:text><xsl:value-of select="$m"/><xsl:text>分钟和</xsl:text><xsl:value-of select="$s"/><xsl:text>秒</xsl:text></持续时间></xsl:copy></xsl:模板><xsl:template name="dateTime-to-seconds"><xsl:param name="dateTime"/><xsl:变量名=日期"select="substring-before($dateTime, 'T')";/><xsl:变量名=时间"select="substring-after($dateTime, 'T')";/><xsl:variable name="local-time";select="substring($time, 1, string-length($time) - 6)";/><xsl:variable name="offset";select="substring-after($time, $local-time)";/><xsl:变量名=年"select="substring($date, 1, 4)";/><xsl:变量名=月份"select="substring($date, 6, 2)";/><xsl:variable name="day";select="substring($date, 9, 2)";/><xsl:变量名=小时"select="substring($local-time, 1, 2)";/><xsl:变量名=分钟"select="substring($local-time, 4, 2)";/><xsl:变量名=第二个"select="substring($local-time, 7)";/><xsl:variable name="offset-sign";选择=1 - 2 * 开始($偏移,'-')";/><xsl:variable name="offset-hour";select=substring($offset, 2, 2) * $offset-sign";/><xsl:variable name="offset-minute";select=substring($offset, 5, 2) * $offset-sign";/><xsl:变量名=a"select="floor((14 - $month) div 12)"/><xsl:变量名=y"select=$year + 4800 - $a"/><xsl:变量名=m";select=$month + 12*$a - 3"/><xsl:variable name="jd";选择=$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 - 3600*$offset-hour - 60*$offset-minute";/></xsl:模板></xsl:stylesheet>

演示:http://xsltfiddle.liberty-development.net/aiyndK>

I have an XML file which includes some dates with start and end points, like shown as follows:

<start time="2016-02-21T00:59:06+02:00"/>
.....
.....
<end time="2016-02-22T02:24:38+02:00"/>

Question:

How to calculate the difference between two time attributes?

解决方案

I am getting the error: ... xsl:version: only 1.0 features are supported

Here's a purely XSLT 1.0 solution:

<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" />
    <xsl:variable name="d" select="floor($duration div 86400)"/>
    <xsl:variable name="t" select="$duration mod 86400"/>
    <xsl:variable name="h" select="floor($t div 3600)"/>
    <xsl:variable name="r" select="$t mod 3600"/>
    <xsl:variable name="m" select="floor($r div 60)"/>
    <xsl:variable name="s" select="$r mod 60"/>
    
    <xsl:copy>
        <xsl:copy-of select="name"/>
        <duration>
            <xsl:value-of select="$d"/>
            <xsl:text> days, </xsl:text>
            <xsl:value-of select="$h"/>
            <xsl:text> hours, </xsl:text>
            <xsl:value-of select="$m"/>
            <xsl:text> minutes and </xsl:text>
            <xsl:value-of select="$s"/>
            <xsl:text> seconds</xsl:text>
        </duration>
    </xsl:copy>
</xsl:template>

<xsl:template name="dateTime-to-seconds">
    <xsl:param name="dateTime"/>

    <xsl:variable name="date" select="substring-before($dateTime, 'T')" />
    <xsl:variable name="time" select="substring-after($dateTime, 'T')" />

    <xsl:variable name="local-time" select="substring($time, 1, string-length($time) - 6)" />
    <xsl:variable name="offset" select="substring-after($time, $local-time)" />

    <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="offset-sign" select="1 - 2 * starts-with($offset, '-')" />
    <xsl:variable name="offset-hour" select="substring($offset, 2, 2) * $offset-sign" />
    <xsl:variable name="offset-minute" select="substring($offset, 5, 2) * $offset-sign" />

    <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 - 3600*$offset-hour - 60*$offset-minute" />
</xsl:template> 

</xsl:stylesheet>

Demo: http://xsltfiddle.liberty-development.net/aiyndK

这篇关于在 XSLT 中查找两个日期时间之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆