xslt: time ago test
using xslt how do i test if a date was within the last (say) 15 days?
input:
- date in format dd/mm/yy
- X number of days
output:
- if the date occured within X days of now
eg recent('02/07/10',30) would return true iff 02/07/10 was 30 days in the past
some steps i got:
main func
<xsl:function name="custom:monthtodays">
<xsl:param name="date"/>
<xsl:param name="daysago"/>
<xsl:variable name="daycountnow" select="year-from-dateTime(current-dateTime())*365+day-from-dateTime(current-dateTime())+custom:monthtodays(month-from-dateTime(current-dateTime())" />
<xsl:variable name="datedaycount" select="numeric(substring($date,1,2))+numeric(substring($date,7,2))*365+custom:monthtodays(numeric(substring($date,4,2)))" />
<xsl:value-of select="$daycountnow - $datedaycount - $daysago > 0"/&开发者_运维问答gt;
</xsl:function>
helper func
<xsl:function name="custom:monthtodays">
<xsl:param name="month"/>
<xsl:choose>
<xsl:when test="$month =1"> <xsl:value-of select="0"/> </xsl:when>
<xsl:when test="$month =2"> <xsl:value-of select="31"/> </xsl:when>
<xsl:when test="$month =3"> <xsl:value-of select="59"/> </xsl:when>
<xsl:when test="$month =4"> <xsl:value-of select="90"/> </xsl:when>
<xsl:when test="$month =5"> <xsl:value-of select="120"/> </xsl:when>
<xsl:when test="$month =6"> <xsl:value-of select="151"/> </xsl:when>
<xsl:when test="$month =7"> <xsl:value-of select="181"/> </xsl:when>
<xsl:when test="$month =8"> <xsl:value-of select="212"/> </xsl:when>
<xsl:when test="$month =9"> <xsl:value-of select="243"/> </xsl:when>
<xsl:when test="$month =10"> <xsl:value-of select="273"/> </xsl:when>
<xsl:when test="$month =11"> <xsl:value-of select="304"/> </xsl:when>
<xsl:when test="$month =12"> <xsl:value-of select="334"/> </xsl:when>
</xsl:choose>
</xsl:function>
but this doesnt take account of leap years and the like ... surely there is a betterway?
Here is one way you could do it in XSLT 1.0. As you are using .Net 4.0, you should be available to use the microsoft extension function, and write your own (javascript) function to do the date difference.
Here is the transformation. Note the JavaScript function is quite crude, and assumes the date is in DD/MM/YY format:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://mycompany.com/mynamespace">
<msxsl:script language="javascript" implements-prefix="user">
function datecheck(dateString)
{
// Get today's date
var today = new Date();
// Clear down any time portion
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today.setMilliseconds(0);
// Length of day in milliseconds
var one_day = 1000*60*60*24;
// Convert date string into a date
var day = parseInt(dateString.substring(0, 2), 10);
var month = parseInt(dateString.substr(3, 2), 10);
var year = 2000 + parseInt(dateString.substr(6, 2), 10);
var date = new Date(year, month - 1, day);
// Get date difference
var diff = Math.ceil((today.getTime()-date.getTime()) / one_day);
return (diff <= 30);
}
</msxsl:script>
<xsl:template match="/*">
<xsl:value-of select="user:datecheck(string(.))"/>
</xsl:template>
</xsl:stylesheet>
When you apply it on this input (assuming today is 23/07/2010)
<date>02/07/10</date>
You should get the return value of true
When you apply it on this input
<date>02/06/10</date>
You should get the return value of false
This transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="my:my">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select="my:isWithinDays(/*, 30)"/>
</xsl:template>
<xsl:function name="my:isWithinDays" as="xs:boolean">
<xsl:param name="pDate" as="xs:string"/>
<xsl:param name="pDaysDuration" as="xs:integer"/>
<xsl:variable name="vvalidDate" select=
"concat('20',
substring($pDate,7),
'-',
substring($pDate,4,2),
'-',
substring($pDate,1,2))"/>
<xsl:sequence select=
"current-date() - xs:date($vvalidDate)
le
xs:dayTimeDuration(concat('P',$pDaysDuration, 'D'))"/>
</xsl:function>
</xsl:stylesheet>
when applied on this XML document:
<t>02/07/10</t>
produces the wanted result:
true
When applied on this XML document:
<t>20/06/10</t>
again the correct result is produced:
false
Note: this transformation was performed today, 21/07/10.
精彩评论