xsl 1.0上的日期操作

问题描述 投票:1回答:2

我需要在变量中定义的给定日期之前和之后3天获取,并将它们中的每一个存储在xsl 1.0中的新单个变量中。我不能使用任何扩展或第三方工具。

通过论坛中的答案,我发现了:Expanding datetime ranges in XSLT 1.0的类似问题,但我不完全明白它是否以及如何适用于我的代码。

Mi日期变量采用标准的dateTime格式,如下所示:

<xsl:variable name="Date" select="2014-05-13T00:00:00"/>

我需要输出类似于此的html:

<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 -->

在具有定价信息的行中,我将必须使用每个单独的日期来执行其他操作,因此每天必须存储在变量中以供进一步使用。

有没有办法实现这一点,只使用xslt 1.0?

提前致谢。

xml datetime xslt-1.0 operation
2个回答
9
投票

在纯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>

- 请注意,givenDate的参数值是一个字符串,因此必须用单引号括起来。


0
投票

SAXON 6.5.5支持EXSLT extensions,但来自date:add-duration()dates and times module - 它可以优雅地解决你的问题 - 没有实现。

但是,你可以直接use Java objects from within XSLT with Saxon

您还可以使用绑定外部Java类的快捷方法,使类名称成为命名空间URI的一部分。

使用快捷方式技术,命名空间的URI标识将找到外部函数的类。名称空间URI必须是"java:",后跟完全限定的类名(例如xmlns:date="java:java.util.Date")...

引用this post,在Java中添加日期的方法是

String dt = "2008-01-01";  // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1);  // number of days to add
dt = sdf.format(c.getTime());  // dt is now the new date

XSLT版本看起来像这样(未经测试):

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:simple-date-format:="java:java.text.SimpleDateFormat"
  xmlns:calendar="java:java.util.Calendar"
>

  <xsl:template name="date-add-days">
    <xsl:param name="input" />
    <xsl:param name="days" />

    <xsl:if test="
      function-available('simple-date-format:new') 
      and function-available('calendar:get-instance')
    ">
      <xsl:variable name="sdf"  select="simple-date-format:new('yyyy-MM-dd')" />
      <xsl:variable name="cal"  select="calendar:get-instance()" />

      <xsl:variable name="time" select="simple-date-format:parse($sdf, $input)" />
      <xsl:variable name="tmp1" select="calendar:set-time($cal, $time)" />
      <xsl:variable name="tmp2" select="calendar:add($cal, calendar:DATE(), number($days))" />
      <xsl:variable name="res" select="calendar:get-time($cal)" />

      <xsl:value-of select="simple-date-format:format($sdf, $res)" />
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

关于与XPath中的Java类和对象进行交互:

我不确定方法名称格式。 Saxon 6.5.5文档似乎暗示了虚线格式(toString()变成了to-string()),所以我一直在这里使用它。也许calendar:set-time()实际上必须打电话给calendar:setTime(),尝试一下并修复我的答案。

© www.soinside.com 2019 - 2024. All rights reserved.