2009-02-16 48 views
4

我正在創建一個網站,我需要顯示RSS feed中的前5條記錄,並且這些記錄需要按日期和時間排序。使用XSL按日期排序RSS feed的問題

RSS feed中的日期字段的格式如下: 「星期一,2009年2月16日16時02分44秒格林尼治標準時間」

我有很大的問題得到記錄,以正確地排序 - 我我已經嘗試了很多我見過的不同的代碼示例,但似乎沒有正確地對記錄進行排序。我的XSL工作表的代碼如下所示,所涉及的訂閱源爲 here

非常感謝任何人的幫助!

謝謝,

Chris。


XSL代碼:

<xsl:stylesheet version="1.1" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:digg="http://digg.com//docs/diggrss/" 
xmlns:dc="http://purl.org/dc/elements/1.1/"> 
<xsl:template match="/">  
<xsl:for-each select="//*[local-name()='item'][position() < 6]">  
    <p> 
     <a> 
     <xsl:attribute name="href"> 
     <xsl:value-of select="*[local-name()='link']"/></xsl:attribute> 
     <xsl:attribute name="target"> 
     <xsl:text>top</xsl:text> 
     </xsl:attribute> 
     <xsl:value-of select="*[local-name()='title']"/> 
     </a> 
     <br/> 
    <span class="smaller"><xsl:value-of select="*[local-name()='pubDate']" disable-output-escaping="yes"/></span> 
    </p> 
</xsl:for-each> 

+0

你還沒有提供你想要完成排序的示例XML(RSS)文件,也沒有提供確切的輸出想要被製作。請編輯你的問題並提供這兩件重要的事情。然後很多人將能夠提供幫助。這並不具有挑戰性。 – 2009-02-17 03:53:21

回答

8

以下變換

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

    <xsl:variable name="vrtfMonths"> 
    <m name="Jan" num="01"/> 
    <m name="Feb" num="02"/> 
    <m name="Mar" num="03"/> 
    <m name="Apr" num="04"/> 
    <m name="May" num="05"/> 
    <m name="Jun" num="06"/> 
    <m name="Jul" num="07"/> 
    <m name="Aug" num="08"/> 
    <m name="Sep" num="09"/> 
    <m name="Oct" num="10"/> 
    <m name="Nov" num="11"/> 
    <m name="Dec" num="12"/> 
    </xsl:variable> 

    <xsl:variable name="vMonths" select= 
    "document('')/*/xsl:variable 
        [@name='vrtfMonths']/*" 
    /> 

    <xsl:template match="channel"> 
     <xsl:for-each select="item"> 
     <xsl:sort data-type="number" order="descending" select= 
     "concat(substring(pubDate,13,4), 
       $vMonths[@name 
         = 
         substring(current()/pubDate,9,3)]/@num, 

       substring(pubDate,6,2), 
       translate(substring(pubDate,18,8), 
          ':', 
          '' 
         ) 
       ) 
     "/> 

     <xsl:if test="not(position() > 5)"> 
      <xsl:copy-of select="."/> 

     </xsl:if> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

當在提供源XML文檔施加(太長列出他再次,看link

產生想要的結果

<item xmlns:media="http://search.yahoo.com/mrss"> 
     <title>Inflation rate expected to fall</title> 
     <description>The latest figures for the rate of inflation, for the 12 months to January, are expected to show that it has fallen again.</description> 
     <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7893873.stm</link> 
     <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7893873.stm</guid> 
     <pubDate>Tue, 17 Feb 2009 02:58:03 GMT</pubDate> 
     <category>Business</category> 
     <media:thumbnail width="66" height="49" url="http://newsimg.bbc.co.uk/media/images/45483000/jpg/_45483998_cash226.jpg" /> 
    </item><item xmlns:media="http://search.yahoo.com/mrss"> 
     <title>Farming pressures</title> 
     <description>Are farmers being forced to use GM seeds?</description> 
     <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7892328.stm</link> 
     <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7892328.stm</guid> 
     <pubDate>Tue, 17 Feb 2009 00:06:31 GMT</pubDate> 
     <category>Business</category> 
     <media:thumbnail width="66" height="49" url="http://newsimg.bbc.co.uk/media/images/45481000/jpg/_45481936_005666785-1.jpg" /> 
    </item><item xmlns:media="http://search.yahoo.com/mrss"> 
     <title>Engine room?</title> 
     <description>Western Australia's economy may be hitting reverse. </description> 
     <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7840370.stm</link> 
     <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7840370.stm</guid> 
     <pubDate>Tue, 17 Feb 2009 00:05:25 GMT</pubDate> 
     <category>Business</category> 
    </item><item xmlns:media="http://search.yahoo.com/mrss"> 
     <title>Coining it in? </title> 
     <description>Investors turning to old and rare coins in the downturn </description> 
     <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7878606.stm</link> 
     <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7878606.stm</guid> 
     <pubDate>Tue, 17 Feb 2009 00:04:29 GMT</pubDate> 
     <category>Business</category> 
     <media:thumbnail width="66" height="49" url="http://newsimg.bbc.co.uk/media/images/45459000/jpg/_45459789_dsc_4982.jpg" /> 
    </item><item xmlns:media="http://search.yahoo.com/mrss"> 
     <title>Home sales may rise say surveyors</title> 
     <description>Property sales may pick in the next few months, according to the Royal Institution of Chartered Surveyors (Rics).</description> 
     <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7893193.stm</link> 
     <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7893193.stm</guid> 
     <pubDate>Tue, 17 Feb 2009 00:02:19 GMT</pubDate> 
     <category>Business</category> 
     <media:thumbnail width="66" height="49" url="http://newsimg.bbc.co.uk/media/images/45483000/jpg/_45483075_006771638-1.jpg" /> 
    </item> 
+0

輝煌迪米特雷!那正是我一直在尋找的!非常感謝。 – pixelkicks 2009-02-17 08:00:15

0

的其他辦法來解決文件限制是使用以下命令:

<xsl:template name="getMonth"> 
    <xsl:param name="name" /> 
    <xsl:choose> 
     <xsl:when test="$name = 'Jan'">01</xsl:when> 
     <xsl:when test="$name = 'Feb'">02</xsl:when> 
     <xsl:when test="$name = 'Mar'">03</xsl:when> 
     <xsl:when test="$name = 'Apr'">04</xsl:when> 
     <xsl:when test="$name = 'May'">05</xsl:when> 
     <xsl:when test="$name = 'Jun'">06</xsl:when> 
     <xsl:when test="$name = 'Jul'">07</xsl:when> 
     <xsl:when test="$name = 'Aug'">08</xsl:when> 
     <xsl:when test="$name = 'Sep'">09</xsl:when> 
     <xsl:when test="$name = 'Oct'">10</xsl:when> 
     <xsl:when test="$name = 'Nov'">11</xsl:when> 
     <xsl:when test="$name = 'Dec'">12</xsl:when> 
     <xsl:otherwise>99</xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

使用名爲

<xsl:call-template name="getMonth"> 
     <xsl:with-param name="name" select="substring(./pubDate,9,3)" /> 
    </xsl:call-template> 

這並不漂亮,但至少你不必擔心安全問題