XSLT 分页 - 默认为当前日期

问题描述 投票:0回答:2

我正在使用 xslt 转换来显示一长串事件。 它具有分页功能,但我希望它默认显示最接近当前日期的第一个事件。

xslt pagination
2个回答
1
投票

我假设您有一个有用的日期格式(YYYY-MM-DD)。

<xsl:param name="currentDate" select="''" /><!-- fill this from outside! -->

<xsl:template name="isPageSelected"><!-- returns true or false -->
  <xsl:param name="eventsOnPage" /><!-- expects a node-set of events -->

  <xsl:choose>
    <xsl:when test="$eventsOnPage">
      <!-- create a string "yyyy-mm-dd,YYYY-MM-DD-" (note the trailing dash) -->
      <xsl:variable name="dateRange">
        <xsl:for-each select="$eventsOnPage">
          <xsl:sort select="date" />
          <xsl:if test="position() = 1">
            <xsl:value-of select="concat(date, ',')" />
          </xsl:if>
          <xsl:if test="position() = last()">
            <xsl:value-of select="concat(date, '-')" />
          </xsl:if>
        </xsl:for-each>
      </xsl:variable>
      <!-- trailing dash ensures that the "less than" comparison succeeds -->
      <xsl:value-of select="
        $currentDate &gt;= substring-before($dateRange, ',')
        and
        $currentDate &lt; substring-after($dateRange, ',')
      " />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="false()" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

因此,在您的分页例程中,要查明当前页面是否是选定的页面,请调用

<xsl:variable name="isPageSelected">
  <xsl:call-template name="isPageSelected">
    <xsl:with-param name="eventsOnPage" select="event[whatever]" />
  </xsl:call-template>
</xsl:variable>
<!-- $isPageSelected now is true or false, proceed accordingly -->

0
投票

由于 XSLT 1.0 中的 sorting 非常非常糟糕,所以最好的选择是在源 XML 中找到一个扩展或包含一个 Unix 风格的时间,这样您就可以对其进行排序(尽管 ISO 格式的字符串也可以工作) ).

© www.soinside.com 2019 - 2024. All rights reserved.