使用 XSLT-1 测试 XML 周中或周末的数据?

Testing XML data for being midweek or weekend with XSLT-1?

采用以下 XML 示例:

<Meeting BookmarkId="0" PageBreak="0" NumberClasses="1" SpecialEvent="1">
    <Date ThisWeek="W20200406" NextWeek="W20200413">April 6-12</Date>
    <SpecialEvent>
        <Event>Memorial</Event>
        <Location>Address goes here</Location>
        <Date Day="7" DayShort="Tue" DayFull="Tuesday" Month="4" MonthShort="Apr" MonthFull="April" Year="2020">07/04/2020</Date>
    </SpecialEvent>
</Meeting>

请记住,此 XML 内容是为大约 50 种语言自动创建的,因此用于星期几的区域设置明显不同。

是否可以使用 XSLT-1 以编程方式确定日期是周中还是周末(不考虑数据的区域设置)?

如果需要,我将更改创建 XML 的应用程序逻辑,以包含一个新的布尔属性,说明现在是周中还是周末。但我想知道使用 XSL if 条件发短信是否容易。

试试这样的东西:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="SpecialEvent/Date">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:variable name="day-of-week">
            <xsl:call-template name="day-of-week">
                <xsl:with-param name="year" select="@Year"/>
                <xsl:with-param name="month" select="@Month"/>
                <xsl:with-param name="day" select="@Day"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:attribute name="Weekend">
            <xsl:value-of select="$day-of-week &lt; 2"/>    
        </xsl:attribute>        
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template name="day-of-week">
    <!-- http://en.wikipedia.org/wiki/Zeller%27s_congruence -->
    <xsl:param name="year" />
    <xsl:param name="month"/>
    <xsl:param name="day"/>
    <!-- m is the month (3 = March, 4 = April, 5 = May, ..., 14 = February) -->
    <xsl:variable name="a" select="$month &lt; 3"/>
    <xsl:variable name="m" select="$month + 12*$a"/>
    <xsl:variable name="y" select="$year - $a"/>
    <xsl:variable name="K" select="$y mod 100"/>
    <xsl:variable name="J" select="floor($y div 100)"/>
    <!--  h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday) -->
    <xsl:variable name="h" select="($day + floor(13*($m + 1) div 5) + $K + floor($K div 4) + floor($J div 4) - 2*$J) mod 7"/>
    <xsl:value-of select="$h"/> 
</xsl:template>

</xsl:stylesheet>

已添加:

how I might tweak it to see Monday as the 1st day of the week and Sunday as the 7th

您可以在输出之前简单地平移结果。而不是:

<xsl:value-of select="$h"/> 

制作模板输出:

<xsl:value-of select="($h + 5) mod 7 + 1"/>