在xsl 1.0上进行日期操作 [英] Date operations on xsl 1.0
问题描述
我需要在变量中定义的给定日期之前和之后的3天,并将它们中的每一个存储在xsl 1.0中的一个新的单独变量中. 我不能使用任何扩展程序或第三方工具.
I need to get 3 days before and after a given date defined in a variable, and store each one of them in a new individual variable in xsl 1.0. i can't use any extensions or third party tools.
在论坛中寻找答案,我发现了这一点:扩展日期时间范围在XSLT 1.0 中遇到了类似的问题,但是我不完全了解它是否以及如何适用于我的代码.
Looking trough the answers in the forums, i found this: Expanding datetime ranges in XSLT 1.0 for a similar problem, but i dont fully understand if and how it would aply to my code.
Mi date变量采用标准dateTime格式,如下所示:
Mi date variable is in standard dateTime format, like this:
<xsl:variable name="Date" select="2014-05-13T00:00:00"/>
我将需要输出类似于以下内容的html:
And i would need to output an html similar to this:
<table>
<tr>
<td>
2014-05-10
<td>
</tr>
<!---some rows with pricing information -->
</table>
<table>
<tr>
<td>
2014-05-11
<td>
</tr>
<!---some rows with pricing information -->
</table>
<table>
<tr>
<td>
2014-05-12
<td>
</tr>
<!---some rows with pricing information -->
</table>
<!-- etc -->
在带有定价信息的行中,我将不得不使用每个单独的日期来执行其他操作,因此,必须将每一天存储在变量中以备将来使用.
In the rows with pricing information I will have to use each individual date to perform other operations, so each day must be stored in a variable for further use.
有没有办法仅使用xslt 1.0来完成此任务?
Is there a way to accomplish this, using just xslt 1.0?
谢谢.
推荐答案
在纯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:param name="givenDate" select="'2014-05-13T00:00:00'"/>
<xsl:param name="daysDiff" select="-3"/>
<xsl:variable name="JDN">
<xsl:call-template name="JDN">
<xsl:with-param name="date" select="$givenDate" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="newDate">
<xsl:call-template name="GD">
<xsl:with-param name="JDN" select="$JDN + $daysDiff" />
</xsl:call-template>
</xsl:variable>
<xsl:template match="/">
<output>
<GivenDate><xsl:value-of select="$givenDate"/></GivenDate>
<NewDate><xsl:value-of select="$newDate"/></NewDate>
</output>
</xsl:template>
<xsl:template name="JDN">
<xsl:param name="date"/>
<xsl:param name="year" select="substring($date, 1, 4)"/>
<xsl:param name="month" select="substring($date, 6, 2)"/>
<xsl:param name="day" select="substring($date, 9, 2)"/>
<xsl:param name="a" select="floor((14 - $month) div 12)"/>
<xsl:param name="y" select="$year + 4800 - $a"/>
<xsl:param name="m" select="$month + 12*$a - 3"/>
<xsl:value-of select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
</xsl:template>
<xsl:template name="GD">
<xsl:param name="JDN"/>
<xsl:param name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
<xsl:param name="e" select="4*$f + 3"/>
<xsl:param name="g" select="floor(($e mod 1461) div 4)"/>
<xsl:param name="h" select="5*$g + 2"/>
<xsl:param name="D" select="floor(($h mod 153) div 5 ) + 1"/>
<xsl:param name="M" select="(floor($h div 153) + 2) mod 12 + 1"/>
<xsl:param name="Y" select="floor($e div 1461) - 4716 + floor((14 - $M) div 12)"/>
<xsl:param name="MM" select="substring(100 + $M, 2)"/>
<xsl:param name="DD" select="substring(100 + $D, 2)"/>
<xsl:value-of select="concat($Y, '-', $MM, '-', $DD)" />
</xsl:template>
</xsl:stylesheet>
结果:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<GivenDate>2014-05-13T00:00:00</GivenDate>
<NewDate>2014-05-10</NewDate>
</output>
-
请注意,givedDate的参数值是 string ,因此必须用单引号引起来.
--
Note that the givenDate's parameter value is a string and as such must be wrapped in single quotes.
这篇关于在xsl 1.0上进行日期操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!