我想在传递给originalDate
模板的变量DAYFORMAT
参数的偏移量中添加任何内容。
这是XML:
<StartDate xmlns:b="http://schemas.datacontract.org/2004/07/System">
<b:DateTime>2020-10-27T23:00:00Z</b:DateTime>
<b:OffsetMinutes>60</b:OffsetMinutes>
</StartDate>
这是我的代码,用于格式化日期,但是我不确定如何向DateTime添加偏移分钟?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="StartDate">
<date>
<display>
<xsl:call-template name='DAYFORMAT'>
<xsl:with-param name="originalDate" select="node()"/>
</xsl:call-template>
</display>
</date>
</xsl:template>
<xsl:template name="DAYFORMAT">
<!-- Takes Original Date in 2017-01-01 format, and converts to e.g. Wed -->
<xsl:param name="originalDate"/>
<xsl:variable name="day" select="substring($originalDate,9,2)"/>
<xsl:variable name="month" select="substring($originalDate,6,2)"/>
<xsl:variable name="year" select="substring($originalDate,1,4)"/>
<!-- eg. 10 Jan 2017 -->
<xsl:variable name="Y">
<xsl:choose>
<xsl:when test="$month < 3">
<xsl:value-of select="$year - 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$year + 0"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="y" select="$Y mod 100"/>
<xsl:variable name="c" select="floor($Y div 100)"/>
<xsl:variable name="d" select="$day+0"/>
<xsl:variable name="m">
<xsl:choose>
<xsl:when test="$month < 3">
<xsl:value-of select="$month + 12"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$month + 0"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="w" select="($d + floor(($m + 1) * 2.6) + $y + floor($y div 4) + floor($c div 4) - $c * 2 - 1) mod 7"/>
<xsl:variable name="www">
<xsl:choose>
<xsl:when test="$w = 0">Sun</xsl:when>
<xsl:when test="$w = 1">Mon</xsl:when>
<xsl:when test="$w = 2">Tue</xsl:when>
<xsl:when test="$w = 3">Wed</xsl:when>
<xsl:when test="$w = 4">Thu</xsl:when>
<xsl:when test="$w = 5">Fri</xsl:when>
<xsl:when test="$w = 6">Sat</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="monthName">
<xsl:choose>
<xsl:when test="$month=01">January</xsl:when>
<xsl:when test="$month=02">February</xsl:when>
<xsl:when test="$month=03">March</xsl:when>
<xsl:when test="$month=04">April</xsl:when>
<xsl:when test="$month=05">May</xsl:when>
<xsl:when test="$month=06">June</xsl:when>
<xsl:when test="$month=07">July</xsl:when>
<xsl:when test="$month=08">August</xsl:when>
<xsl:when test="$month=09">September</xsl:when>
<xsl:when test="$month=10">October</xsl:when>
<xsl:when test="$month=11">November</xsl:when>
<xsl:when test="$month=12">December</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$www"/>
<xsl:text></xsl:text>
<xsl:value-of select="$day"/>
<xsl:text></xsl:text>
<xsl:value-of select="$monthName"/>
<xsl:text></xsl:text>
</xsl:template>
</xsl:stylesheet>
我知道我可以使用它来添加,但是这次,我陷入了1.0版
<xsl:variable name="adjusted-dateTime" select="xs:dateTime(b:DateTime) + b:OffsetMinutes * xs:dayTimeDuration('PT1M')"/>
以下是期望的结果:
<Display>Sat 28 November</Display>
我该怎么做?
答案 0 :(得分:1)
这是XSLT 1.0中的大量工作。实际上,该问题应适当地分为两个部分:
以下样式表会同时执行以下操作:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:b="http://schemas.datacontract.org/2004/07/System"
exclude-result-prefixes="b">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="StartDate">
<!-- 1. convert dateTime to seconds -->
<xsl:variable name="dateTime-in-seconds">
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="b:DateTime"/>
</xsl:call-template>
</xsl:variable>
<!-- 2. add offset and convert to date -->
<xsl:variable name="total-seconds" select="$dateTime-in-seconds + 60 * b:OffsetMinutes" />
<xsl:variable name="JDN" select="floor($total-seconds div 86400)" />
<xsl:variable name="new-date">
<xsl:call-template name="JDN-to-Gregorian">
<xsl:with-param name="JDN" select="$JDN"/>
</xsl:call-template>
</xsl:variable>
<!-- 3. format new date -->
<Display>
<!-- 3.1 day name -->
<xsl:variable name="dayOfweek" select="($JDN + 1) mod 7"/>
<xsl:value-of select="substring('SunMonTueWedThuFriSat', 3 * $dayOfweek + 1, 3)"/>
<xsl:text> </xsl:text>
<!-- 3.2 day number -->
<xsl:value-of select="number(substring($new-date, 9, 2))"/>
<xsl:text> </xsl:text>
<!-- 3.3 month name -->
<xsl:variable name="month" select="substring($new-date, 6, 2)"/>
<xsl:choose>
<xsl:when test="$month=1">January</xsl:when>
<xsl:when test="$month=2">February</xsl:when>
<xsl:when test="$month=3">March</xsl:when>
<xsl:when test="$month=4">April</xsl:when>
<xsl:when test="$month=5">May</xsl:when>
<xsl:when test="$month=6">June</xsl:when>
<xsl:when test="$month=7">July</xsl:when>
<xsl:when test="$month=8">August</xsl:when>
<xsl:when test="$month=9">September</xsl:when>
<xsl:when test="$month=10">October</xsl:when>
<xsl:when test="$month=11">November</xsl:when>
<xsl:when test="$month=12">December</xsl:when>
</xsl:choose>
</Display>
</xsl:template>
<xsl:template name="dateTime-to-seconds">
<xsl:param name="dateTime"/>
<xsl:variable name="date" select="substring-before($dateTime, 'T')" />
<xsl:variable name="time" select="substring-after($dateTime, 'T')" />
<xsl:variable name="year" select="substring($date, 1, 4)" />
<xsl:variable name="month" select="substring($date, 6, 2)" />
<xsl:variable name="day" select="substring($date, 9, 2)" />
<xsl:variable name="hour" select="substring($time, 1, 2)" />
<xsl:variable name="minute" select="substring($time, 4, 2)" />
<xsl:variable name="second" select="substring($time, 7, 2)" />
<xsl:variable name="a" select="floor((14 - $month) div 12)"/>
<xsl:variable name="y" select="$year + 4800 - $a"/>
<xsl:variable name="m" select="$month + 12*$a - 3"/>
<xsl:variable name="jd" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
<xsl:value-of select="86400*$jd + 3600*$hour + 60*$minute + $second" />
</xsl:template>
<xsl:template name="JDN-to-Gregorian">
<xsl:param name="JDN"/>
<xsl:variable name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
<xsl:variable name="e" select="4*$f + 3"/>
<xsl:variable name="g" select="floor(($e mod 1461) div 4)"/>
<xsl:variable name="h" select="5*$g + 2"/>
<xsl:variable name="D" select="floor(($h mod 153) div 5 ) + 1"/>
<xsl:variable name="M" select="(floor($h div 153) + 2) mod 12 + 1"/>
<xsl:variable name="Y" select="floor($e div 1461) - 4716 + floor((14 - $M) div 12)"/>
<xsl:value-of select="$Y" />
<xsl:value-of select="format-number($M, '-00')"/>
<xsl:value-of select="format-number($D, '-00')"/>
</xsl:template>
</xsl:stylesheet>