使用 XSLT 获取月份的第一个和最后一个星期日
Get First and Last Sunday in Month with XSLT
我正在尝试将 UTC 格式的 XML 日期转换为多个时区。我将能够使用下面的行来获取它,但我无法计算 summertime/winter 次的差异。有什么方法可以让我获得一个月中的第一个 Sunday/last 星期日?
xs:dateTime(/root/field/@myField)+xs:dayTimeDuration('PT1H')
xs:dateTime(/root/field/@myField)+xs:dayTimeDuration('PT2H')
其中 XML 将是:
<root>
<field myField="2002-12-24T12:00:12Z"/>
</root>
谢谢!
这里有一种方法可以计算给定日期所在月份的第一个星期日:
XML
<input>
<event date="2020-01-20"/>
<event date="2020-02-03"/>
<event date="2020-03-17"/>
<event date="2020-04-29"/>
<event date="2020-05-09"/>
<event date="2020-06-11"/>
<event date="2020-07-06"/>
<event date="2020-08-15"/>
<event date="2020-09-28"/>
<event date="2020-10-01"/>
<event date="2020-11-30"/>
<event date="2020-12-17"/>
</input>
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"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/input">
<results>
<xsl:for-each select="event">
<xsl:variable name="date" select="xs:date(@date)"/>
<xsl:variable name="day1" select="$date - (day-from-date($date) - 1) * xs:dayTimeDuration('P1D')"/>
<xsl:variable name="weekday" select="xs:integer(format-date($day1, '[F1]'))"/>
<first-sunday>
<xsl:value-of select="$day1 + (7 - $weekday) * xs:dayTimeDuration('P1D')"/>
</first-sunday>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="utf-8"?>
<results>
<first-sunday>2020-01-05</first-sunday>
<first-sunday>2020-02-02</first-sunday>
<first-sunday>2020-03-01</first-sunday>
<first-sunday>2020-04-05</first-sunday>
<first-sunday>2020-05-03</first-sunday>
<first-sunday>2020-06-07</first-sunday>
<first-sunday>2020-07-05</first-sunday>
<first-sunday>2020-08-02</first-sunday>
<first-sunday>2020-09-06</first-sunday>
<first-sunday>2020-10-04</first-sunday>
<first-sunday>2020-11-01</first-sunday>
<first-sunday>2020-12-06</first-sunday>
</results>
我正在尝试将 UTC 格式的 XML 日期转换为多个时区。我将能够使用下面的行来获取它,但我无法计算 summertime/winter 次的差异。有什么方法可以让我获得一个月中的第一个 Sunday/last 星期日?
xs:dateTime(/root/field/@myField)+xs:dayTimeDuration('PT1H')
xs:dateTime(/root/field/@myField)+xs:dayTimeDuration('PT2H')
其中 XML 将是:
<root>
<field myField="2002-12-24T12:00:12Z"/>
</root>
谢谢!
这里有一种方法可以计算给定日期所在月份的第一个星期日:
XML
<input>
<event date="2020-01-20"/>
<event date="2020-02-03"/>
<event date="2020-03-17"/>
<event date="2020-04-29"/>
<event date="2020-05-09"/>
<event date="2020-06-11"/>
<event date="2020-07-06"/>
<event date="2020-08-15"/>
<event date="2020-09-28"/>
<event date="2020-10-01"/>
<event date="2020-11-30"/>
<event date="2020-12-17"/>
</input>
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"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/input">
<results>
<xsl:for-each select="event">
<xsl:variable name="date" select="xs:date(@date)"/>
<xsl:variable name="day1" select="$date - (day-from-date($date) - 1) * xs:dayTimeDuration('P1D')"/>
<xsl:variable name="weekday" select="xs:integer(format-date($day1, '[F1]'))"/>
<first-sunday>
<xsl:value-of select="$day1 + (7 - $weekday) * xs:dayTimeDuration('P1D')"/>
</first-sunday>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="utf-8"?>
<results>
<first-sunday>2020-01-05</first-sunday>
<first-sunday>2020-02-02</first-sunday>
<first-sunday>2020-03-01</first-sunday>
<first-sunday>2020-04-05</first-sunday>
<first-sunday>2020-05-03</first-sunday>
<first-sunday>2020-06-07</first-sunday>
<first-sunday>2020-07-05</first-sunday>
<first-sunday>2020-08-02</first-sunday>
<first-sunday>2020-09-06</first-sunday>
<first-sunday>2020-10-04</first-sunday>
<first-sunday>2020-11-01</first-sunday>
<first-sunday>2020-12-06</first-sunday>
</results>