如何计算 xslt 1.0 中的时间差(持续时间)? [英] How to calculate the time difference (duration) in xslt 1.0?

查看:38
本文介绍了如何计算 xslt 1.0 中的时间差(持续时间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 xlstproc 将一些 xml 文件转换为另一个 xml 格式文件,在转换过程中,我需要计算开始和结束字段之间的时间差值(以秒或分钟为单位:秒).

I use xlstproc to transform some xml file to another xml format file, during transformation, I need to calculate the time difference value (durations in seconds or minutes:seconds) between start and end fields.

<代码>...<start>2011-12-13 16:15:26</start><end>2011-12-13 16:17:27</end>...

我找到了模板语法,但没有使用它.

I found a Template Syntax, but failed to make use of it.

<xsl:call-template name="date:duration">
  <xsl:with-param name="seconds" select="number" />?
</xsl:call-template>

希望有人能给我提示如何实现我的目标.提前致谢!

Would appreciate somebody can give me a hint how to achieve my goal. Thanks in advance!

推荐答案

我为你做了一些工作,发现 这个日期:持续时间函数,您似乎正在尝试使用它.但是,date:duration 将秒数转换为持续时间格式的字符串,而您想要找到两个日期时间字符串之间的差异(持续时间).

I did some of your work for you and found this date:duration function, which you seem to be trying to use. However, date:duration converts a number of seconds into a duration formatted string, whereas you want to find the difference (duration) between two datetime strings.

您可能想要 date:difference.如果您阅读了此函数/模板的文档,您将找到关于参数的这个:

You probably want date:difference instead. If you read the documentation for this function/template, you'll find this about the arguments:

这两个日期必须都是右截断的日期/时间字符串之一[XML 模式第 2 部分:数据类型] 中定义的格式....允许的格式如下...

The two dates must both be right-truncated date/time strings in one of the formats defined in [XML Schema Part 2: Datatypes]. ... The permitted formats are as follows...

xs:dateTime (CCYY-MM-DDThh:mm:ss)
...

原文中有斜体:CCYY-MM-DDThh:mm:ss 除了 T 没有斜体.换句话说,时间字符串在日期和时间之间需要一个文字 T,而你的有一个空格.

There are italics there in the original: CCYY-MM-DDThh:mm:ss except the T is not italicized. In other words, the time strings need a literal T between the date and the time, whereas yours have a space.

所以我建议解决这个问题:

So I would suggest fixing that:

<start>2011-12-13T16:15:26</start>
  <end>2011-12-13T16:17:27</end>

startend 字符串作为参数传递给模板.您只需传递 startend 元素节点即可完成此操作,这些节点将根据其文本内容自动转换为字符串:

Pass the start and end strings as parameters to the template. You can do this by just passing the start and end element nodes, which will be automatically converted to strings based on their text content:

<xsl:variable name="time-diff-dur">
  <xsl:call-template name="date:difference">
    <xsl:with-param name="start" select="start" />
    <xsl:with-param name="end" select="end" />
  </xsl:call-template>
</xsl:variable>
<!-- The above returns a duration formatted string, so convert that to seconds: -->
<xsl:variable name="time-diff-sec">
  <xsl:call-template name="date:seconds">
    <xsl:with-param name="seconds" select="$time-diff-dur" />?
  </xsl:call-template>
</xsl:variable>

此代码假定上下文节点是 元素的父节点.在上面的代码之后,变量$time-diff-sec 将包含一个结果树片段,可以使用number($time-diff-sec) 如有必要.

This code assumes that the context node is the parent of the <start> and <end> elements. After the above code, the variable $time-diff-sec will contain a result tree fragment, which can be converted to a number using number($time-diff-sec) if necessary.

让我们知道这是否有效.如果不是,请具体说明结果是什么以及它与您预期的有何不同.

Let us know whether that works. If not, state specifically what the result was and how it differs from what you expected.

我刚刚注意到您正在使用 xsltproc(使用 libxslt).根据本文档,libxslt 支持 date:difference(和 date:seconds)本机.因此,您可以将这些函数作为函数调用,而不是定义一个命名模板并将其作为模板调用.这对您来说会少很多代码,尽管可移植性较差:

I just noticed that you are using xsltproc (which uses libxslt). According to this documentation, libxslt supports date:difference (and date:seconds) natively. So you can call these functions as functions instead of defining a named template and calling it as a template. That would be a lot less code for you, albeit less portable:

<xsl:variable name="time-diff-sec"
    select="date:seconds(date:difference(start, end))" />

和以前一样,您需要在某处声明 date 命名空间前缀,通常在您的 xsl:stylesheet 元素上:

As before, you will need to declare the date namespace prefix somewhere, usually on your xsl:stylesheet element:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:date="http://exslt.org/dates-and-times"
    extension-element-prefixes="date">

更新 2:

请参阅此答案,了解便携式 XSLT 1.0 模板将日期字符串转换为秒数,这使您可以轻松地从另一个日期中减去一个日期.(感谢 Sam B 的评论).

Update 2:

See this answer for a portable XSLT 1.0 template to convert a date string to number of seconds, which allows you to easily subtract one date from another. (Thanks to Sam B for that comment).

这篇关于如何计算 xslt 1.0 中的时间差(持续时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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