XSLT 1.0比较日期

时间:2013-12-03 04:17:32

标签: date xslt xslt-1.0

我想将当前日期与开始日期和结束日期进行比较。

XML是:

<forms>
  <form id="11">
     <start>somedate</start>
     <end>someotherdate</end>
   </form>
</forms>

我正在尝试:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex" >

<xsl:choose>
   <xsl:when test="end != ''                
      and start != '' 
      and ex:date(start) &lt;= current-date() 
      and current-date() &lt;= ex:date(end)">
      <!-- Do Stuff -->
   </xsl:when>
</xsl:choose>

我收到以下错误:

xmlXPathCompOpEval: function current-date not found
XPath error : Unregistered function
XPath error : Stack usage errror
<ul class="ccb_forms_ul"></ul>

1 个答案:

答案 0 :(得分:6)

假设您的日期采用yyyy-mm-dd格式(据我所知,它们来自您的其他问题),这应该适用于大多数 XSLT 1.0处理器:

<?xml version="1.0" encoding="UTF-8"?>
<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">

...

<xsl:for-each select="forms/form">

<xsl:variable name="today" select="translate(substring-before(date:date-time(), 'T'), '-', '')"/>
<xsl:variable name="start" select="translate(start, '-', '')"/>
<xsl:variable name="end" select="translate(end, '-', '')"/>

<xsl:if test="$start &lt;= $today and $today &lt;= $end">
    <!-- in progress -->
</xsl:if>