我如何比较 xsl 中日期的年份?

How do l compare year of a date in xsl?

如何将给定日期的年份与整数进行比较?

<if test="year(@varDate)>=1900">
true
</if>

输入文件中日期的格式为:

<date varDate="31/12/1999" />

如果您受困于 XSLT 1.0,最常见的方法是提取年份部分,如下所示:

substring-before('1994-12-31', '-')

或者,如果您的格式是 dd/MM/yyyy,您可以执行 1:

substring-after('31/12/1999', '/')

在你的情况下,应该是这样的:

<xsl:if test="substring-after('31/12/1999', '/') > 1900">
    true
</xsl:if>

既然你没有提供输入数据,我想你可以自己算出细节。您的原始问题被标记为 , but this has since changed. If that means that you can use XSLT 2.0 or higher, and you have an actual typed xs:date value, you can also use fn:year-from-date:

<xsl:if test="year-from-date(xs:date('1994-12-31')) > 1900">
    true
</xsl:if>

1 这还不够,因为有两个斜杠,使用 substring-after(substring-after('31/12/1999', '/'), '/'),如 所示(归功于他)。

it turns out my date is in a different format that in solution above (dd/mm/yyyy).

如果那是真的你的日期格式 - i.e.both 天和月总是填充到两位数 - 你可以使用:

<if test="substring(@varDate, 7) >= 1900">

作为你的测试。

如果没有,您需要:

<if test="substring-after(substring-after(@varDate, '/'), '/') >= 1900">