XSLT将毫秒转换为时间码

  • 本文关键字:时间 转换 XSLT xslt
  • 更新时间 :
  • 英文 :


大家好,祝大家今天愉快!

我想弄清楚有两个步骤:

  1. (但不是优先级)从带有"duration"标签的xml文件中获取整数以毫秒为单位
  2. 我正在尝试转换"持续时间";毫秒转换为时间码,看起来像hh:mm:ss:ff,其中h -小时,m -分钟,s -秒和f -帧(25帧=1秒)。我看到的算法是:
z=milliseconds
h=z idiv (60*60*25)
m=(z-h*60*60*25) idiv (60*25)
s=(z-h*60*60*25-m*60*25) idiv 25
f=(z-h*60*60*25-m*60*25-s*25)

知道如何在XSLT中正确地进行计算吗?

考虑以下示例:

XML>
<input>
<milliseconds>45045500</milliseconds>
</input>

XSLT 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:template match="input">
<output>            
<xsl:variable name="f" select="floor(milliseconds div 40) mod 25" />
<xsl:variable name="s" select="floor(milliseconds div 1000) mod 60" />
<xsl:variable name="m" select="floor(milliseconds div 60000) mod 60" />
<xsl:variable name="h" select="floor(milliseconds div 3600000)" />
<timecode>
<xsl:value-of select="format-number($h, '00')"/>
<xsl:value-of select="format-number($m, ':00')"/>
<xsl:value-of select="format-number($s, ':00')"/>
<xsl:value-of select="format-number($f, ':00')"/>
</timecode>
</output>
</xsl:template>
</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<output>
<timecode>12:30:45:12</timecode>
</output>

注意,这个截断小数帧。如果你想要四舍五入到最近的一帧,那么将变量更改为:

<xsl:variable name="totalFrames" select="round(milliseconds div 40)" />
<xsl:variable name="f" select="$totalFrames mod 25" />
<xsl:variable name="s" select="floor($totalFrames div 25) mod 60" />
<xsl:variable name="m" select="floor($totalFrames div 1500) mod 60" />
<xsl:variable name="h" select="floor($totalFrames div 90000)" />

这里的结果是:

<timecode>12:30:45:13</timecode>

在XSLT 2.0或更高版本中,可以将表达式floor($a div $b)缩短为$a idiv $b

最新更新