【问题标题】:XSL Iterate through list of itemsXSL 遍历项目列表
【发布时间】:2017-04-27 13:55:35
【问题描述】:

我有一个 RSS 提要,它来自一个废弃的“今日笑话”博客。由于博客不再更新,我想遍历列表并每天显示不同的项目,最好按时间顺序显示。

如何识别列表中的“第一个”项目(最旧的帖子),然后每天显示下一个项目?

RSS 源在这里:http://feeds.feedburner.com/DailyJokes-ACleanJokeEveryday?format=xml 完整列表在这里:http://dailyjokes.somelifeblog.com/

我的 XSL 代码在这里,当前显示位置 2 的项目,这是最近的帖子:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
  <xsl:template match="/">
    <xsl:apply-templates select="//item[position() &lt; 2]"/>
  </xsl:template>

  <xsl:template match="item">
    <content-item>
      <h1><xsl:value-of select="substring-before(title, ' #Joke')"/></h1>     
      <p><xsl:value-of select="substring-before(description, '&lt;a href')" disable-output-escaping="yes"/></p>
    </content-item>
  </xsl:template>
</xsl:stylesheet>

我在 SharePoint 2013 RSS webpart 中显示此提要

我的目标是每天显示不同的项目,但我可能会接受简单的随机化。此外,如果有人能推荐一个适合我工作 Intranet 的免费或收费的“今日笑话”网站,我们将不胜感激!

【问题讨论】:

  • XSLT 1.0 没有 current-date() 或 random() 函数,因此这是不可能的,除非您从外部提供这些值之一 - 使用参数或扩展名。我不熟悉 SharePoint,所以我不知道其中哪些是可能的。
  • 如果涉及撒克逊人,您可以使用 xmlns:math="java.lang.Math" 和 select="math:random()" 。就像@michael.hor257k 说的那样,不要认为有一个可以使用的内置选项
  • SharePoint 似乎支持 Today 函数作为扩展:msdn.microsoft.com/en-us/library/dd583143(office.11).aspx。因此,您可以获取日期,convert it to a serial number 并使用结果项目数作为要获取的项目的索引。

标签: loops xslt rss sharepoint-2013


【解决方案1】:

我的目标是每天展示不同的商品

为了实现您的目标,您的处理器需要知道当前日期。

以下是一个最小化的示例,展示了如何使用日期来每天检索不同的项目:

XML

<rss>
    <channel>
        <item>
            <title>Alpha</title>
        </item>
        <item>
            <title>Bravo</title>
        </item>
        <item>
            <title>Charlie</title>
        </item>
        <item>
            <title>Delta</title>
        </item>
        <item>
            <title>Echo</title>
        </item>
    </channel>
</rss>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="current-datetime">2017-04-27T00:00:00</xsl:param>

<xsl:template match="/rss">
    <xsl:variable name="JDN">
        <xsl:call-template name="JDN">
            <xsl:with-param name="date" select="$current-datetime"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="n" select="count(channel/item)" />
    <output>
        <xsl:apply-templates select="channel/item[$JDN mod $n + 1]"/>
    </output>
</xsl:template>

<xsl:template match="item">
    <item>
        <xsl:value-of select="title"/>
    </item>     
</xsl:template>

<xsl:template name="JDN">
    <xsl:param name="date"/>
    <xsl:param name="year" select="substring($date, 1, 4)"/>
    <xsl:param name="month" select="substring($date, 6, 2)"/>
    <xsl:param name="day" select="substring($date, 9, 2)"/>
    <xsl:param name="a" select="floor((14 - $month) div 12)"/>
    <xsl:param name="y" select="$year + 4800 - $a"/>
    <xsl:param name="m" select="$month + 12*$a - 3"/>
    <xsl:value-of select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
</xsl:template> 

</xsl:stylesheet>

在本例中,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <item>Bravo</item>
</output>

现场演示:http://xsltransform.net/pNmBxZK


如果您的处理器支持 EXSLT date:date-time()extension 函数,您可以将样式表的标题修改为:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="current-datetime" select="date:date-time()"/>

对于SharePoint,我猜你需要使用 ddwrt 命名空间中的TodayIso 函数。

【讨论】:

  • 我能够按照您的建议使用TodayISO 实现今天的日期:&lt;xsl:variable name="todayDateTime" select="ddwrt:TodayIso()"/&gt; 并将以下内容添加到样式表属性中:xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:msxsl="urn:schemas-microsoft-com:xslt" 我将您的逻辑实现到我的解决方案中并使用@ 987654334@ 参数设置为今天 2017 年 5 月 2 日,提要返回第二个位置的项目。能否请您介绍一下 JDN 逻辑,以便我了解此日期如何返回此结果?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多