【问题标题】:XSL parsing and sorting by dateXSL 解析和按日期排序
【发布时间】:2013-07-23 03:02:51
【问题描述】:

我有这个现有的 xsl,用于呈现 RSS 提要。
除了标题之外,其他所有内容都从描述字段中解析出来。
下面的 sn-p 是目前的完成方式。它可以工作,但没有设置项目的顺序。 如何修改我拥有的内容,以便按itemPubDate 对项目进行排序。 示例 itemPubDate 值为 06/26/2013

<xsl:template name="RSSMainTemplate.body">
    <xsl:param name="Rows" />
    <xsl:param name="RowCount" />
    <xsl:for-each select="$Rows">
        <xsl:variable name="CurPosition" select="position()" />
        <xsl:variable name="RssFeedLink" select="$rss_ID" />
        <xsl:variable name="CurrentElement" select="concat($RssFeedLink,$CurPosition)" />
        <xsl:call-template name="RSSDescTransform1">
            <xsl:with-param name="DescriptionField" select="description" />
        </xsl:call-template>
    </xsl:for-each>
</xsl:template>

<xsl:template name="RSSDescTransform1">
    <xsl:param name="DescriptionField" />
    <xsl:variable name="itemTitle" select="title" />
    <xsl:variable name="itemAuthor" select="substring-before(substring-after($DescriptionField, 'itemAuthor:&lt;/b&gt; '),'&lt;/div&gt;')" />
    <xsl:variable name="itemPubDate" select="substring-before(substring-after($DescriptionField, 'itemPubDate:&lt;/b&gt; '),'&lt;/div&gt;')" />
    <xsl:variable name="itemDescription" select="substring-before(substring-after($DescriptionField, 'itemDescription:&lt;/b&gt; '),'&lt;/div&gt;')" />
    <xsl:variable name="itemHTML" select="substring-after($DescriptionField, 'itemHTML:&lt;/b&gt; ')" />
    <xsl:variable name="itemLink" select="substring-before(substring-after($DescriptionField, 'href=&quot;'),'&quot;')" />
    <xsl:call-template name="RSSDescTransform2">
        <xsl:with-param name="itemTitle" select="$itemTitle" />
        <xsl:with-param name="itemAuthor" select="$itemAuthor" />
        <xsl:with-param name="itemPubDate" select="$itemPubDate" />
        <xsl:with-param name="itemDescription" select="$itemDescription" />
        <xsl:with-param name="itemLink" select="$itemLink" />
        <xsl:with-param name="itemHTML" select="$itemHTML" /></xsl:call-template>
</xsl:template>

完整的 XSL here

我目前没有输入样本,但它看起来像

<item>
  <title>lorem ipsum</title>
  <description>
    <![CDATA[
      <div>
        itemAuthor:<b>John</b>
        itemPubDate:<b>06/26/2013</b>
        ...
      </div>
    ]]>
  <description>
</item>
<item>
  ...
</item>

输出几乎相同,只是添加了一些 CSS 样式。


XSL 现在不会导致任何错误。我只是想让项目按itemPubDate 排序

【问题讨论】:

  • 如果没有输入 XML 和期望输出示例,真的很难说出问题所在。您可以将&lt;xsl:sort&gt;&lt;xsl:apply-templates&gt; 一起使用,但不能与呼叫一起使用,而且您有两个级别的&lt;xsl:call-template&gt; 的事实让我认为您的XSL 还有其他问题。
  • @LegoStormtroopr 添加了更多信息

标签: xslt xml-parsing


【解决方案1】:

您应该能够在 RSSMainTemplate.body 模板中的 for-each 中应用排序。 关键是提取日期部分(年、月、日),并分别对每个部分进行排序

<xsl:for-each select="$Rows">
       <xsl:sort select="substring(substring-before(substring-after(description, 'itemPubDate:&lt;b&gt;'),'&lt;/b&gt;'),7,4)"  />
       <xsl:sort select="substring(substring-before(substring-after(description, 'itemPubDate:&lt;b&gt;'),'&lt;/b&gt;'),4,2)"  />
       <xsl:sort select="substring(substring-before(substring-after(description, 'itemPubDate:&lt;b&gt;'),'&lt;/b&gt;'),1,2)"  />

我还注意到您的选择语句中存在一个问题,即从您拥有的 div 中提取信息(基于您的示例 XML)

<xsl:variable name="itemPubDate" select="substring-before(substring-after($DescriptionField, 'itemPubDate:&lt;/b&gt; '),'&lt;/div&gt;')" />

应该是

<xsl:variable name="itemPubDate" select="substring-before(substring-after($DescriptionField, 'itemPubDate:&lt;b&gt;'),'&lt;/b&gt;')" />

【讨论】:

  • 这行得通,谢谢!实际上select语句是正确的,我只是输入错误。它应该看起来像&lt;div&gt;&lt;b&gt;itemPubDate:&lt;/b&gt; 22/07/2013&lt;/div&gt;。我已经相应地修改了你的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多