【问题标题】:Testing XML data for being midweek or weekend with XSLT-1?使用 XSLT-1 测试 XML 数据是在周中还是周末?
【发布时间】:2020-02-08 17:31:25
【问题描述】:

采用以下 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 条件发短信是否容易。

【问题讨论】:

  • 可以 - 虽然不容易 - 从日期本身(即从日、月和年属性)计算星期几。但是,尚不清楚什么是“周中”以及什么是“周末”——尤其是。当你说你正在处理不同的语言环境时。
  • @michael.hor257k 周中 - 周一至周五;周末 - 周六和周日
  • @michael.hor257k 如果这太难了,或者语言环境带来了额外的复杂性,那么也许只是以编程方式添加一个新属性将是最简单的方法。

标签: xml xslt-1.0


【解决方案1】:

尝试类似:

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>

补充:

我如何调整它以将星期一视为一周中的第一天,将星期日视为第 7 天

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

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

制作模板输出:

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

【讨论】:

  • 谢谢。我做了一些调整来测试您提供的核心 day-of-week 模板并且它可以工作。谢谢。
  • 这是一个非常有用的模板。您能否指出我如何调整它以将星期一视为一周中的第一天,将星期日视为第七天。谢谢。
  • @ChrisGilbert 查看我的答案的补充。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多