如何比较日期xsl

时间:2016-03-30 20:56:21

标签: xml xslt

我想使用xslt列出所有日期大于7月5日的电影。这就是我尝试过的:

   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="1.0">
   <xsl:template match="/">
   <html>
     <body>
       <table border="1">
        <xsl:for-each select="cinema/movie">
        <xsl:if text="date>7 May">
        <tr>
         <td>
           <xsl:value-of select="title"/>
         </td>
       </tr>
       </xsl:if>
       </xsl:for-each>
      </table>
    </body>
   </html>
  </xsl:template> 
  </xsl:stylesheet>

如何解决这个问题?谢谢。

修改

  <?xml version="1.0"?> 
  <?xml-stylesheet type="text/css" href="movie.css" ?>
    <cinema>
    <movie>
    <title>The hobbit</title>
    <date>9 July</date>
    <duration> 134 minutes</duration>
</movie> 
</cinema>

1 个答案:

答案 0 :(得分:3)

XSLT 1.0没有日期概念 - 而且9 July不是日期。如果您想将给定值与5月7日(任何年份!)进行比较,请尝试以下方法:

...
<xsl:for-each select="cinema/movie">
    <xsl:variable name="d" select="substring-before(date, ' ')" />  
    <xsl:variable name="mmm" select="substring(substring-after(date, ' '), 1, 3)" />    
    <xsl:variable name="m" select="string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec', $mmm)) div 3 + 1" />
    <xsl:if test="$m + 0.01 * $d > 5.07">
        <tr>
            <td>
                <xsl:value-of select="title"/>
            </td>
        </tr>
    </xsl:if>
</xsl:for-each>
...