使用XSL按RFC-822日期格式对XML进行排序

时间:2013-02-21 21:31:40

标签: xml xslt rfc822

有没有人知道在XSL中进行这种排序的方法?

这是我到目前为止所得到的,但它只是白天排序而忽略了剩下的日期。

      <xsl:apply-templates select="item">
          <xsl:sort select="pubDate" data-type="text" order="descending"/>
      </xsl:apply-templates>

1 个答案:

答案 0 :(得分:2)

感谢快速回复的人。让我朝着正确的方向前进管理解决它!找到了有用的链接http://blog.mastykarz.nl/how-to-do-it-rss-aggregation-merging-multiple-xml-files-using-xslt/

我使用的是XSLT 2.0版。只是一个案例,使用变量在MMM月份中替换并将日期细分为日期。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="Months" select="'Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec'"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>

<xsl:template match="channel">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()[not(preceding-sibling::item) and not(self::item)]"/>
        <xsl:apply-templates select="item">
            <!-- year -->
        <xsl:sort select="substring(pubDate, 13, 4)" order="descending" data-type="number"/>
        <!-- month -->
        <xsl:sort select="string-length(substring-before($Months, substring(pubDate, 9, 3)))" order="descending" data-type="number"/>
        <!-- day -->
        <xsl:sort select="substring(pubDate, 6, 2)" order="descending" data-type="number"/>
        <!-- hour -->
        <xsl:sort select="substring(pubDate, 18, 2)" order="descending" data-type="number"/>

        </xsl:apply-templates>
        <xsl:apply-templates select="@*|node()[not(following-sibling::item) and not(self::item)]"/>
</xsl:copy>
</xsl:template>

相关问题