XSLT 1.0 Compare Dates

2020-03-25 05:07发布

问题:

I want to compare current date against a start date and end date.

XML is:

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

I am currently trying:

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

I get the following error:

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

回答1:

Assuming your dates are in yyyy-mm-dd format (as I understand they are from your other question), this should work with most XSLT 1.0 processors:

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