如何添加持续时间和打印结果保留区域偏移?

时间:2016-02-18 11:38:38

标签: xml xslt xslt-1.0 exslt

我正在尝试使用xslt转换xml文件。这是一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<testFile>
   <action name="foo" duration="PT600S" endTime="2016-02-05T13:30:00.084+01:00"/>
   <action name="bar" duration="PT600S" endTime="2016-02-05T13:45:00.084+01:00"/>
</testFile>

对于每个action代码,我需要将startTime计算为endTime - duration。这是我的方法:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:date="http://exslt.org/dates-and-times"
                extension-element-prefixes="date"
                version="1.0">
     <xsl:output method="text" indent="yes"/>
     <xsl:variable name="sep">;</xsl:variable>
     <xsl:variable name="newLine"><xsl:text>&#xa;</xsl:text></xsl:variable>

     <xsl:template match="text()" />

     <xsl:template match="testFile/action">
       <xsl:value-of select="concat(@name,$sep)" />
       <xsl:variable name="start" select="date:add(@endTime,concat('-',@duration))" />
       <xsl:value-of select="concat($start,$sep,@endTime)" />
       <xsl:value-of select="$newLine" />
       <xsl:value-of select="concat(@name,$sep)" />
       <xsl:variable name="normDate" select="substring-before(@endTime,'.')"/>
       <xsl:variable name="start1" select="date:add($normDate,concat('-',@duration))" />
       <xsl:value-of select="concat(substring-before($start1,'Z'),'.',substring-after(@endTime,'.'),$sep,@endTime)" />
       <xsl:value-of select="$newLine" />
     </xsl:template>
</xsl:stylesheet>

结果:

$ xsltproc data-test.xslt data-test.xml
foo;2016-02-05T12:20:00.083999999999833Z;2016-02-05T13:30:00.084+01:00
foo;2016-02-05T13:20:00.084+01:00;2016-02-05T13:30:00.084+01:00
bar;2016-02-05T12:35:00.083999999999833Z;2016-02-05T13:45:00.084+01:00
bar;2016-02-05T13:35:00.084+01:00;2016-02-05T13:45:00.084+01:00
$

我想获得每个操作的第二个结果,但我不确定我的实现是否正确且独立于我的代码运行的国家/地区。

如何在endTime中添加持续时间并以与endTime相同的格式打印出startTime?

以下是xsltproc版本:

$ xsltproc --version
Using libxml 20626, libxslt 10117 and libexslt 813
xsltproc was compiled against libxml 20626, libxslt 10117 and libexslt 813
libxslt 10117 was compiled against libxml 20626
libexslt 813 was compiled against libxml 20626

0 个答案:

没有答案