XSLT日期比较 [英] XSLT date comparison
问题描述
早上好,
i已经检查了许多关于这个主题的回应,但没有成功...非常抱歉...
我有一个xml文件(seance)的元素courses(seances):(我已删除不必要的细节)
....
< seances>
< seance date =2014-09-10T00:00:01>
... a'seance'的详细信息
< / seance>
< seance date =2013-09-10T00:00:01>
...
< / seance>
... other'seance'elements
< / seances>
,我的xslt样式表生成一个html文档:
< xsl:stylesheet xmlns:xsl =http://www.w3.org/1999/XSL/Transformversion =2.0
xmlns:date =http://exslt.org/dates-and-times
extension-element-prefixes =date
xmlns:gr =http://www.w3.org/2000/ svg>
< xsl:output method =htmlencoding =iso-8859-1indent =yes/>
< xsl:import href =date.xsl/>
记住日期:
< xsl:variable name =ddjas =xs:dateTimeselect =date:date-time()/>
我想在课程日期确定时显示一个元素课程不是在将来):
我已经尝试过很多这样的条件,但它总是false:
< xsl:template match =seance>
< xsl:variable name =dateseanceas =xs:dateTimeselect =@ date/>
< xsl:value-of select =$ dateseance/>< xsl:value -of select =$ ddj/>
< xsl:choose>
< xsl:when test =$ ddj& gt; = $ dateeance>
< xsl:text> -OK-< / xsl:text>
< / xsl:when>
< xsl:otherwise>
< xsl:text> - 不确定 - < / xsl:text>
< / xsl:otherwise>
< / xsl:choose>
< / xsl:template>
日期似乎确定,但它打印:
2014-09-10T00:00:012013-09-11T10:00:00.004 + 02:00-不OK - (不会打印!)
其他行应为确定:
2013-09-10T00:00:012013-09-11T10:00:00.004 + 02:00-不OK -09-10T00:00:012013-09-11T10:00:00.004 + 02:00-不OK- 2012-09-10T00:00:012013-09-11T10:00:00.004 + 02:00-不OK- 2012 -09-10T00:00:012013-09-11T10:00:00.004 + 02:00-不OK- 2012-09-10T00:00:012013-09-11T10:00:00.004 + 02:00-不OK- / p>
我希望可以理解...
非常感谢任何帮助。
A精度:我在mozilla / firefox中处理它
下面是一个XSLT 1.0忽略时区并假定两个日期/时间值都在同一时区的解决方案:
T:\ftemp> type dates.xml
< seances>
< seance date =2014-09-10T00:00:01>
... a'seance'的详细信息
< / seance>
< seance date =2013-09-10T00:00:01>
...
< / seance>
... other'seance'elements
< / seances>
T:\ftemp> xslt dates.xml dates.xsl
2014-09-10T00:00:01 2013-09-11T14:30:13不OK -
2013-09-10T00:00:01 2013-09-11T14:30:13 -OK-
T:\ftemp> type dates.xsl
< xsl:stylesheet xmlns :xsl =http://www.w3.org/1999/XSL/Transform
xmlns:date =http://exslt.org/dates-and-times
extension-element -prefixes =date
version =1.0
xmlns:gr =http://www.w3.org/2000/svg>
< xsl:output method =htmlencoding =iso-8859-1indent =yes/>
< xsl:variable name =ddjselect =substring(date:date-time(),1,19)/>
< xsl:template match =seances>
< xsl:apply-templates select =seance/>
< / xsl:template>
< xsl:template match =seance>
< xsl:variable name =dateseanceselect =@ date/>
< xsl:value-of select =$ dateseance/>
< xsl:text> < / xsl:text>
< xsl:value-of select =$ ddj/>
< xsl:text> < / xsl:text>
< xsl:choose>
< xsl:when test =translate($ ddj,':T-','')> =
translate($ datedeance,':T - ','')
< xsl:text> -OK-< / xsl:text>
< / xsl:when>
< xsl:otherwise>
< xsl:text> - 不确定 - < / xsl:text>
< / xsl:otherwise>
< / xsl:choose>
< xsl:text>& #xa;< / xsl:text>
< / xsl:template>
< / xsl:stylesheet>
T:\ftemp>
这是因为它将日期/时间值转换为数字,在XSLT 1.0中,使用>
和<
。
Good morning, i have checked many responses about this topic, but without success...very sorry...
i have got an xml document with an element 'courses' ("seances") which has 'course' éléments ("seance") : (i have deleted unnecessary details)
....
<seances>
<seance date="2014-09-10T00:00:01">
...details in a 'seance'
</seance>
<seance date="2013-09-10T00:00:01">
...
</seance>
...other 'seance' elements
</seances>
and my xslt stylesheet produces an html document :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date"
xmlns:gr="http://www.w3.org/2000/svg" >
<xsl:output method="html" encoding="iso-8859-1" indent="yes" />
<xsl:import href="date.xsl" />
to memorize the date of the day :
<xsl:variable name="ddj" as="xs:dateTime" select="date:date-time()"/>
i would like to show only an element course when the date of the course is ok (the date is not in the future) :
i have tried many conditions like this one but it is always false :
<xsl:template match="seance">
<xsl:variable name="dateseance" as="xs:dateTime" select="@date"/>
<xsl:value-of select="$dateseance" /><xsl:value-of select="$ddj" />
<xsl:choose>
<xsl:when test="$ddj >= $dateseance">
<xsl:text>-OK-</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>-Not OK-</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Dates seems to be ok, but it prints :
2014-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- (will not be printed !)
other lines should be "ok" :
2013-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- 2012-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- 2012-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- 2012-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- 2012-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK-
i hope that it can be understood...
many thanks for any help.
A precision : i process it in mozilla/firefox
Below is an XSLT 1.0 solution that ignores time zones and assumes both date/time values are in the same time zone:
T:\ftemp>type dates.xml
<seances>
<seance date="2014-09-10T00:00:01">
...details in a 'seance'
</seance>
<seance date="2013-09-10T00:00:01">
...
</seance>
...other 'seance' elements
</seances>
T:\ftemp>xslt dates.xml dates.xsl
2014-09-10T00:00:01 2013-09-11T14:30:13 -Not OK-
2013-09-10T00:00:01 2013-09-11T14:30:13 -OK-
T:\ftemp>type dates.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date"
version="1.0"
xmlns:gr="http://www.w3.org/2000/svg" >
<xsl:output method="html" encoding="iso-8859-1" indent="yes" />
<xsl:variable name="ddj" select="substring(date:date-time(),1,19)"/>
<xsl:template match="seances">
<xsl:apply-templates select="seance"/>
</xsl:template>
<xsl:template match="seance">
<xsl:variable name="dateseance" select="@date"/>
<xsl:value-of select="$dateseance" />
<xsl:text> </xsl:text>
<xsl:value-of select="$ddj" />
<xsl:text> </xsl:text>
<xsl:choose>
<xsl:when test="translate($ddj,':T-','') >=
translate($dateseance,':T-','')">
<xsl:text>-OK-</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>-Not OK-</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
T:\ftemp>
This works because it converts the date/time values into numbers which, in XSLT 1.0, can be compared using >
and <
.
这篇关于XSLT日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!