2012-04-19 86 views
2

我想測試組中符合city和current-date()的事件,以便輸出標題。XSLT:如何測試是否存在至少一個有資格的子節點/ @ date

要找到城市($ place eq // event/@ city)似乎可行。但我無法弄清楚如何表達「一些eventTime/@日期小於$今天」。錯誤消息是「一個不止一個項目的序列不被允許作爲'eq'的第二個操作數'」這是令人困惑的,因爲我寫了test =「($ place eq // event/@ city)和(xs:日期(今天$)LT XS:?日期(// EVENTTIME/@日期))

我應該如何被今天比較$在我EVENTTIME的@date這裏的輸入

<calendar> 
<group month="2012-04-01"> 
    <event city="paris"> 
     <eventTime date="2012-04-02"/> 
     <eventText>Paris - expired April date</eventText> 
    </event> 
    <event city="london"> 
     <eventTime date="2012-04-19"/> 
     <eventText>London - current April 19 date</eventText> 
    </event> 
    <event city="london"> 
     <eventTime date="2012-04-24"/> 
     <eventText>London - current April date</eventText> 
    </event> 
</group> 
<group month="2012-05-01"> 
    <event city="london"> 
     <eventTime date="2012-05-02"/> 
     <eventText>London - current May date</eventText> 
    </event> 
    <event city="paris"> 
     <eventTime date="2012-05-01"/> 
     <eventText>Paris - current May date</eventText> 
    </event> 
    <event city="london"> 
     <eventTime date="2012-05-02"/> 
     <eventText>London - current May date</eventText> 
    </event> 
</group> 

</calendar> 

這裏的。在XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       xmlns:fn="http://johnadamturnbull.com/xslt" 
       exclude-result-prefixes="xs" 
       version="2.0" > 

    <xsl:output method="html" indent="yes" name="html"/> 

    <xsl:param name="place" as="xs:string" required="yes"></xsl:param> 
    <xsl:variable name="today" select="current-date()" as="xs:date"/> 

     <xsl:template match="/"> 
     <html> 
      <body> 
     <xsl:apply-templates select="calendar/group"/> 
     </body> 
     </html> 
    </xsl:template> 



    <xsl:template match = "group"> 

     <xsl:if test="($place eq //event/@city) and 
       (xs:date($today) ge xs:date(//eventTime/@date))"> 

       <h4 class = "dateHeader"> 
        <xsl:value-of select="format-date(./@month,'[MNn] [Y]')"/> 
       </h4> 
       <ul> 
        <xsl:apply-templates select="event"></xsl:apply-templates> 
       </ul> 
     </xsl:if> 
    </xsl:template> 

    <xsl:template match="event"> 
     <xsl:variable name="eventTime" select="eventTime/@date" as="xs:date"/> 
     <xsl:choose> 
      <xsl:when test="($eventTime ge $today) and 
          (($place eq @city) or (@city eq ''))"> 
       <li> 
         <xsl:apply-templates></xsl:apply-templates> 
       </li> 
      </xsl:when> 
      </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 
+0

'// event/@ city'正在返回6個項目的序列。你可以發佈你的XSLT,以便我們可以看到你想要完成的事情嗎? – 2012-04-19 17:08:46

+0

我說「少於今天」 - 我的意思是=今天。 – 2012-04-19 17:59:24

回答

1

沒有什麼樣的HTML輸出應該是一個例子,但我敢肯定,我可以告訴你想什麼實現。

我認爲您的XSLT可以通過刪除xsl:ifxsl:choose並添加謂詞來進行測試來簡化。

這XSLT 2.0樣式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:fn="http://johnadamturnbull.com/xslt" exclude-result-prefixes="xs fn" version="2.0"> 
    <xsl:output method="html" indent="yes" name="html"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:param name="place" as="xs:string" required="yes"/> 
    <xsl:variable name="today" select="current-date()" as="xs:date"/> 

    <xsl:template match="/"> 
    <html> 
     <body> 
     <xsl:apply-templates select="calendar/group"/> 
     </body> 
    </html> 
    </xsl:template> 

    <!--Match group if @city matches $place or is empty and has an eventTime 
    with a @date that is greater than or equal to today's date.--> 
    <xsl:template match="group[event[@city=$place or @city=''][xs:date(eventTime/@date) >= $today]]"> 
     <h4 class="dateHeader"> 
     <xsl:value-of select="format-date(@month,'[MNn] [Y]')"/> 
     </h4> 
     <ul> 
     <!--Only apply-templates to events that have a @city that matches $place 
     or has a @city that is empty.--> 
     <xsl:apply-templates select="event[@city=$place or @city='']"/> 
     </ul> 
    </xsl:template> 

    <!--Only match events that have an eventTime with a @date that is greater than 
    or equal to today's date.--> 
    <xsl:template match="event[@city=$place or @city=''][xs:date(eventTime/@date) >= $today]"> 
    <li> 
     <xsl:apply-templates/> 
    </li> 
    </xsl:template> 

    <xsl:template match="event"/> 

</xsl:stylesheet> 

應用到您的示例XML輸入產生此HTML輸出:

<html> 
    <body> 
     <h4 class="dateHeader">April 2012</h4> 
     <ul> 
     <li>London - current April 19 date</li> 
     <li>London - current April date</li> 
     </ul> 
     <h4 class="dateHeader">May 2012</h4> 
     <ul> 
     <li>London - current May date</li> 
     <li>London - current May date</li> 
     </ul> 
    </body> 
</html> 

如果這是不是你要找的,請加一個例子HTML輸出的外觀應該如何。

+0

這正是我需要的輸出。它適用於「倫敦」。但是當我傳入地點參數「巴黎」時,它失敗了。 (我得到的結果是... Paris - expired April date

  • London - current April19 date
  • 2012-04-19 22:02:05

    +0

    @JohnTurnbull - 對不起,我需要在事件匹配中添加$ place謂詞,並且需要添加另一個事件匹配來捕捉流浪漢請看我的編輯 – 2012-04-19 22:22:00

    +0

    這真是太棒了我現在得到謂詞了非常感謝你真的很有用 – 2012-04-19 23:39:26

    相關問題