我想在传递给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>
以下是期望的结果:
<Display>Sat 28 November</Display>
我该怎么做?
答案 0 :(得分:3)
在XSLT 2.0中,这可能很简单:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:b="http://schemas.datacontract.org/2004/07/System"
exclude-result-prefixes="xs b">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="StartDate">
<xsl:variable name="adjusted-dateTime" select="xs:dateTime(b:DateTime) + b:OffsetMinutes * xs:dayTimeDuration('PT1M')"/>
<Display>
<xsl:value-of select="format-dateTime($adjusted-dateTime, '[FNn,*-3] [D] [MNn]')"/>
</Display>
</xsl:template>
</xsl:stylesheet>