比较数组中的 XSL 值

Compare XSL values in array

我不确定如何给它起标题 post..

我想比较行之间的 pubDate

Desired Output: Only print the pubDate when it is different from the previous row's pubDate. (my substring function returns Dec 2014, Jan 2015, etc)

目前我设置为每次都打印 pubDate。我只是不知道如何比较行之间的值...请帮忙!

代码摘录:

<xsl:template match="item">
<xsl:variable name="Rows" select="channel/item"/>
<xsl:variable name="RowCount" select="count($Rows)"/>
<xsl:variable name="item_link" select="link"/>
<xsl:variable name="item_title" select="description"/>

<xsl:value-of select="$Rows"/>
<xsl:variable name="CurPosition" select="position()" />
<xsl:variable name="PrevPosition" select="position()-1" />
<xsl:variable name="RssFeedLink" select="$rss_WebPartID" />
<xsl:variable name="CurrentElement" select="concat($RssFeedLink,$CurPosition)" />
<xsl:if test="not(substring(pubDate,3,9) = 0)">
  <div align="center">
    <hr/>
    <xsl:value-of select="substring(pubDate,3,9)" disable-output-escaping="yes"/>
  </div>
</xsl:if>
<li>
  <a href="{$item_link}" title="{$item_title}">
    <xsl:variable name="SafeHtml">
      <xsl:call-template name="GetSafeHtml">
        <xsl:with-param name="Html" select="title"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$SafeHtml" disable-output-escaping="yes"/>
  </a>
</li>

这里有完整的代码供任何人使用:

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
           version="1.0" exclude-result-prefixes="xsl ddwrt msxsl rssaggwrt"
           xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
           xmlns:rssaggwrt="http://schemas.microsoft.com/WebParts/v3/rssagg/runtime"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
           xmlns:rssFeed="urn:schemas-microsoft-com:sharepoint:RSSAggregatorWebPart">

<xsl:param name="rss_FeedLimit">30</xsl:param>
<xsl:param name="rss_ExpandFeed">true</xsl:param>
<xsl:param name="rss_LCID">1033</xsl:param>
<xsl:param name="rss_WebPartID">RSS_Viewer_WebPart</xsl:param>
<xsl:param name="rss_alignValue">left</xsl:param>
<xsl:param name="rss_IsDesignMode">True</xsl:param>

  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="Rows" select="channel/item"/>
<xsl:variable name="RowCount" select="count($Rows)"/>
<div>
  <xsl:apply-templates select="rss/channel"/>
</div>
</xsl:template>
<xsl:template match="rss/channel">
<xsl:variable name="link" select="link"/>
<xsl:variable name="description" select="description"/>

<ul>
  <xsl:apply-templates select="item"/>
</ul>
</xsl:template>


<xsl:template match="item">
<xsl:variable name="Rows" select="channel/item"/>
<xsl:variable name="RowCount" select="count($Rows)"/>
<xsl:variable name="item_link" select="link"/>
<xsl:variable name="item_title" select="description"/>

<xsl:value-of select="$Rows"/>
<xsl:variable name="CurPosition" select="position()" />
<xsl:variable name="RssFeedLink" select="$rss_WebPartID" />
<xsl:variable name="CurrentElement" select="concat($RssFeedLink,$CurPosition)" />
<xsl:if test="not(substring(pubDate,3,9) = 0)">
  <div align="center">
    <hr/>
    <xsl:value-of select="substring(pubDate,3,9)" disable-output-escaping="yes"/>
  </div>
</xsl:if>
<li>
  <a href="{$item_link}" title="{$item_title}">
    <xsl:variable name="SafeHtml">
      <xsl:call-template name="GetSafeHtml">
        <xsl:with-param name="Html" select="title"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$SafeHtml" disable-output-escaping="yes"/>
  </a>
</li>




</xsl:template>

<xsl:template name="GetSafeHtml">
  <xsl:param name="Html"/>
  <xsl:choose>
   <xsl:when test="$rss_IsDesignMode = 'True'">
     <xsl:value-of select="$Html"/>
   </xsl:when>
   <xsl:otherwise>
     <xsl:value-of select="rssaggwrt:MakeSafe($Html)"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

如果要检查前一个 "row" 的值,可以使用 preceding-sibling 轴运算符。

所以,与其写这个……

<xsl:if test="not(substring(pubDate,3,9) = 0)">

写这个...

<xsl:if test="not(substring(pubDate,3,9) = 0) and not(pubDate = preceding-sibling::item[1]/pubDate)">

注意这里[1]的使用。执行 preceding-sibling 将 return 当前节点之前的所有节点,而您只需要第一个节点。

另请注意,您可能会想写这个...

<xsl:if test="not(substring(pubDate,3,9) = 0) and pubDate != preceding-sibling::item[1]/pubDate">

但这不适用于第一个 item 元素,因为要使该表达式为真,前面的兄弟必须存在。

最后,这只有在 item 元素按 pub-date 顺序排列时才有效。更好的方法是将此视为分组问题,并按日期对项目进行分组。对于 XSLT 1.0,您将使用一种称为 Muenchian Grouping.

的技术