【问题标题】:Problem sorting RSS feed by date using XSL使用 XSL 按日期排序 RSS 提要的问题
【发布时间】:2010-10-07 23:34:04
【问题描述】:

我正在创建一个网站,我需要在其中显示来自 RSS 提要的前 5 条记录,并且这些记录需要按日期和时间排序。

RSS 提要中的日期字段采用以下格式: “格林威治标准时间 2009 年 2 月 16 日星期一 16:02:44”

我在让记录正确排序时遇到了很大的问题——我尝试了很多不同的代码示例,但似乎没有一个可以正确排序记录。我的 XSL 工作表的代码如下所示,有问题的提要是 here.

非常感谢任何人的帮助!!!

谢谢,

克里斯。


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>

【问题讨论】:

  • 您没有提供您希望对其进行排序的示例 XML(RSS) 文件,也没有提供您希望生成的确切输出。请编辑您的问题并提供这两件重要的事情。然后很多人将能够提供帮助。这不是那么具有挑战性。

标签: datetime xslt rss sorting


【解决方案1】:

以下变换

<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>

【讨论】:

  • 辉煌的二米特!这正是我一直在寻找的!非常感谢。
  • 不使用 document('') 可以做同样的事情吗?或者是什么允许 xpath 访问 .xsl 文件中的元素?我之所以问,是因为 DotNetNuke 默认情况下不解析 XSL 文件中的 document('')。
  • @steve-t 可以使用 xxx:node-set() 扩展函数,或者将 $vrtfMonths 变量的内容放在单独的 xml 文件中,然后通过 document(thatFilesURL )
  • @Dimitre 非常感谢,效果很好。 select="ms:node-set($vrtfMonths)/m 而不是 select="document('') 我在这里找到了如何将 MSXML 与 node-set() 一起使用:social.msdn.microsoft.com/forums/en-US/xmlandnetfx/thread/…
  • @Dimitre 谢谢。如果日期没有工作日怎么办?例如 2009 年 2 月 17 日 00:06:31 +0100。你也有这样的代码示例吗?
【解决方案2】:

绕过文档限制的另一种方法是使用以下方法:

<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>

虽然不好看,但至少不用担心安全

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多