2009-12-23 88 views
0

我正在使用xslt轉換來顯示一長串事件。 它有分頁,但我希望它是默認的第一個事件是最接近當前日期。XSLT尋呼 - 默認爲當前日期

+1

沒有XSLT/XML的例子,實在是很難回答這個問題。 – Oded 2009-12-23 14:40:06

回答

0

由於XSLT 1.0中的sorting是可怕的,可怕的損壞,所以最好的辦法是在源XML中的某處找到擴展名或包含Unix風格的時間,以便對此進行排序(儘管ISO格式的字符串也可能工作)。

+1

嘿哈克,你爲什麼認爲xsl排序可怕? – Maleks 2009-12-23 14:49:23

+0

因爲它不能排序任何不是字符串或數字的東西。相反,你必須破解一個字符串表示,按你想要的方式進行排序。 – 2009-12-23 15:14:15

+0

我實際上並不想對輸出進行排序。 它已經。在n個頁面的列表中,我需要轉到包含當前日期範圍的第1頁 – 2009-12-31 22:54:56

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 --> 
相關問題