0

How can the value for <pubDate> originating from many RSS-feeds (in xml) be transformed when the weekday is omitted, followed by a sort on pubDate.

@Dimitre provided a fine solution which includes the weekday in his answer to: Problem sorting RSS feed by date using XSL

The question here is how to adapt the substring() for pubDate in the code of that answer when the weekday is NOT part of pubDate, like so:

17 Feb 2009 00:05:25 +0100

In addition it would be convenient to know how to implement an additional RSS-node to sort on like <link> or <title>. The resulting output for example would be sorted on <pubDate> and <link> (or eg. <title>). Thanks for feedback.

Community
  • 1
  • 1
snahl
  • 497
  • 2
  • 10
  • 25
  • What prevents you from using `substring($string_with_weekday, 6)` or `substring-after($string_with_weekday, ', ')` as the input suggested by @Dimitre? As for your other questions I think it would be better to remove them here and turn them into seperate SO questions (of course, after having done the appropriate research). – Marcus Rickert Apr 21 '14 at 22:37

1 Answers1

0

If all your pubDate's do not have the weekday you would simply substract 5 from each substring starting point in the sort statement:

<xsl:sort data-type="number" order="descending" select=
    "concat(substring(pubDate,8,4),
            $vMonths[@name 
                    = 
                     substring(current()/pubDate,4,3)]/@num,

            substring(pubDate,1,2),
            translate(substring(pubDate,13,8),
                      ':',
                      ''
                      )
            )
     "/>

However if you have a mix of pubDate formats, and you have both:

<pubDate>12 Feb 2009 01:58:03 GMT</pubDate>
<pubDate>Tue, 13 Feb 2010 02:58:03 GMT</pubDate>

The starting points of the substring expressions needs to be conditional depending on your format.

In XSLT 2.0 you could simply use an xpath if then else statement like:

substring(pubDate, if (substring(pubDate,3,1)=' ') then 8 else 13 ,4)

This checks if there is a blank space at char 3, which is the case for the format without the weekday but not for the format with the weekday, and sets the substring starting point according.

In XSLT 1.0 there is no conditional expression within an xpath, but you can get the same result by multiplying with the result of a boolean expression:

substring(pubDate, 
    (substring(pubDate,3,1)=' ') * 8 + (substring(pubDate,5,1)=' ') * 13 
,4)

This will calculate 1 * 8 + 0 * 13 = 8 for the format without the weekday and vice versa 0 * 8 + 1 * 13 = 13 for the format with weekday.

Doing this for the whole sort statement:

    <xsl:sort data-type="number" order="descending" select=
    "concat(substring(pubDate, 
             (substring(pubDate,3,1)=' ') * 8 + (substring(pubDate,5,1)=' ') * 13 
            ,4),
            $vMonths[@name = 
              substring(current()/pubDate,
                (substring(pubDate,3,1)=' ') * 4 + (substring(pubDate,5,1)=' ') * 9
              ,3)]/@num,

            substring(pubDate,
              (substring(pubDate,3,1)=' ') * 1 + (substring(pubDate,5,1)=' ') * 6
            ,2),
            translate(
               substring(pubDate,13
                 (substring(pubDate,3,1)=' ') * 13 + (substring(pubDate,5,1)=' ') * 18
               ,8),
                      ':',
                      ''
                      )
            )
     "/>

Here is a working simplified version: http://xsltransform.net/jyyiVhv


As for your second question, In addition it would be convenient to know how to implement an additional RSS-node to sort on like or . The resulting output for example would be sorted on and (or eg. ).

You usually would simply add <xsl:sort select="title"/> however considering the previous sorting by date, this would only happen if you would have 2 different title's on the exact same pubdate, which is rather impossible since the date also includes time.

Tobias Klevenz
  • 1,625
  • 11
  • 13
  • Thank you for providing the solution for a mix of pubDate formats and that working simplified version. That is excellent. – snahl Apr 22 '14 at 19:39