Formatting RSS dates with format-date.xsl

jbenoit's Avatar

jbenoit

30 Oct, 2014 01:02 PM

I am trying to reformat the dates from an RSS Feed block using the following format:

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:include href="/._formats/Utility/format-date"/>
      <!-- XSLT transformation -->
      <!-- Match on the root channel XML node -->
      <xsl:template match="//channel">
          <xsl:for-each select="item">
              <span class="date">
                  <xsl:call-template name="format-date">
                      <xsl:with-param name="date">
                          <xsl:value-of select="../pubDate"/>
                      </xsl:with-param>
                      <xsl:with-param name="mask">mediumDate</xsl:with-param>
                  </xsl:call-template> - <xsl:value-of select="pubDate"/>
              </span>
              <a href="{link}"><xsl:value-of select="title"/></a>
          </xsl:for-each>
      </xsl:template>
  </xsl:stylesheet>
RSS Feed sample:
  <system-xml>
      <rss version="2.0"
          xmlns:atom="http://www.w3.org/2005/Atom">
          <channel>
              <title>CNU Combined Calendars</title>
              <link>http://calendar.cnu.edu/</link>
              <description/>
              <pubDate>Fri, 06 May 2011 12:31:27 GMT</pubDate>
              <lastBuildDate>Mon, 14 Jan 2013 21:27:37 GMT</lastBuildDate>
              <category>Calendar of Events</category>
              <atom:link href="http://calendar.cnu.edu/RSSFeeds.aspx?data=qaUdY1wvEciJ2wvy%2fMP8sjw%2f7K%2bJD0V6OZKBhz3%2bCnGS1iEBPpNHfg%3d%3d" rel="self" type="application/rss+xml"/>
              <generator>:: Master Calendar ::</generator>
              <copyright>2014</copyright>
              <ttl>30</ttl>
              <item>
                  <title>
                      <![CDATA[Study Abroad 101 - Trible Library 111A]]>
                  </title>
                  <description xml:space="preserve">
                      <![CDATA[ 4:00 PM to 5:00 PM]]>
                  </description>
                  <author>[email blocked] (Study Abroad)</author>
                  <category>(None)</category>
                  <pubDate>Thu, 30 Oct 2014 20:00:00 GMT</pubDate>
                  <link>http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J7hb6HyeMEVPm1RPaMv7mxYtNMXbUTnvRrkWnuNoxud3gvXSK7%2b27RU</link>
                  <guid>http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J7hb6HyeMEVPm1RPaMv7mxYtNMXbUTnvRrkWnuNoxud3gvXSK7%2b27RU</guid>
              </item>
              <item>
                  <title>
                      <![CDATA[The Dept. of Theater & Dance presents: Vanya and Sonia and Masha and Spike - Ferguson Center, Music & Theater Hall]]>
                  </title>
                  <description xml:space="preserve">
                      <![CDATA[ 8:00 PM]]>
                  </description>
                  <author>[email blocked] (Briana Taravella)</author>
                  <category>(None)</category>
                  <pubDate>Fri, 31 Oct 2014 00:00:00 GMT</pubDate>
                  <link>http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J7ZqcL6XLXtC%2bYMShH16KezsuDxr34%2flMjTEBerkZpulRl0NwVeVigV</link>
                  <guid>http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J7ZqcL6XLXtC%2bYMShH16KezsuDxr34%2flMjTEBerkZpulRl0NwVeVigV</guid>
              </item>
              <item>
                  <title>
                      <![CDATA[The Dept. of Theater & Dance presents: Vanya and Sonia and Masha and Spike - Ferguson Center, Music & Theater Hall]]>
                  </title>
                  <description xml:space="preserve">
                      <![CDATA[ 8:00 PM]]>
                  </description>
                  <author>[email blocked] (Briana Taravella)</author>
                  <category>(None)</category>
                  <pubDate>Sat, 01 Nov 2014 00:00:00 GMT</pubDate>
                  <link>http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J4TULhZc%2bMj1jWfkh%2fCSPLaddtvWJSGpQgmcw2LRRv0SjMh1Be9YBjI</link>
                  <guid>http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J4TULhZc%2bMj1jWfkh%2fCSPLaddtvWJSGpQgmcw2LRRv0SjMh1Be9YBjI</guid>
              </item>
When I test the format, the first date which should be transformed by the format-date.xsl only shows the first date. Sample below:
  <span class="date">Oct 30, 2014 - Thu, 30 Oct 2014 20:00:00 GMT</span>
  <a href="http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J7hb6HyeMEVPm1RPaMv7mxYtNMXbUTnvRrkWnuNoxud3gvXSK7%2b27RU">Study Abroad 101 - Trible Library 111A</a>
  <span class="date">Oct 30, 2014 - Fri, 31 Oct 2014 00:00:00 GMT</span>
  <a href="http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J7ZqcL6XLXtC%2bYMShH16KezsuDxr34%2flMjTEBerkZpulRl0NwVeVigV">The Dept. of Theater &amp; Dance presents: Vanya and Sonia and Masha and Spike - Ferguson Center, Music &amp; Theater Hall</a>
  <span class="date">Oct 30, 2014 - Sat, 01 Nov 2014 00:00:00 GMT</span>
  <a href="http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J4TULhZc%2bMj1jWfkh%2fCSPLaddtvWJSGpQgmcw2LRRv0SjMh1Be9YBjI">The Dept. of Theater &amp; Dance presents: Vanya and Sonia and Masha and Spike - Ferguson Center, Music &amp; Theater Hall</a>
  <span class="date">Oct 30, 2014 - Thu, 06 Nov 2014 21:00:00 GMT</span>
  <a href="http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J7hb6HyeMEVPm1RPaMv7mxY8XN1RqlYocIGbb5lOxyv3xrgteMlI3ON">Study Abroad 101 - Trible Library 111A</a>
  <span class="date">Oct 30, 2014 - Fri, 07 Nov 2014 17:00:00 GMT</span>
  <a href="http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J7bS9KgATmCvJBB2p9ywGwRJFzI97y%2bN1a5n86eReq4T%2fGJP%2flfRASt">Trible Library Chamber Music Series, Brass Quintet - Trible Library, Rotunda</a>
  <span class="date">Oct 30, 2014 - Sat, 08 Nov 2014 01:00:00 GMT</span>
  <a href="http://calendar.cnu.edu/EventDetails.aspx?data=hHr80o3M7J6DTb0J22zb%2f15%2frfUtQax8WunJmwJw%2fsI7z%2fO80NBgDOZrUc3X%2folg">Opera CNU presents Donizetti's The Elixir of Love - Music and Theatre Hall</a>
Can I not use the format-date.xsl in a "for-each" loop?
  1. 1 Posted by Ryan Griffith on 30 Oct, 2014 04:10 PM

    Ryan Griffith's Avatar

    Hi,

    It looks like you are supplying the <channel> pubDate as opposed to the <item> pubDate:

    <xsl:value-of select="../pubDate"/>
    

    Because you are iterating through the item elements at this point, this is telling XSLT to go up a level, which is the channel element.

    When you have a moment, try adjusting your Format to the following and let me know how that works out:

    <xsl:value-of select="pubDate"/>
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by jbenoit on 30 Oct, 2014 05:06 PM

    jbenoit's Avatar

    Ryan,

    I made that change, and get the same result.

    Updated code:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:include href="/._formats/Utility/format-date"/>
        <!-- XSLT transformation -->
        <!-- Match on the root channel XML node -->
        <xsl:template match="//channel">
            <xsl:apply-templates select="item"/>
        </xsl:template>
        <xsl:template match="item">
            <span class="date">
                <xsl:call-template name="format-date">
                    <xsl:with-param name="date" select="pubDate"/>
                    <xsl:with-param name="mask">mmm ddd d</xsl:with-param>
                </xsl:call-template> - 
                <xsl:value-of select="pubDate"/>
            </span>
            <a href="{link}">
                <xsl:value-of select="title"/>
            </a>
        </xsl:template>
    </xsl:stylesheet>
    
  3. 3 Posted by Ryan Griffith on 30 Oct, 2014 05:11 PM

    Ryan Griffith's Avatar

    Hi,

    Thank you for following up and my apologies the change did not address the issue.

    Because you are seeing today's date each time, I suspect the issue is the format does not like the value being passed into the date param, so it's falling back to the current date.

    When you have a moment, please also try the following change and let me know if the output is still the same:

    xsl:with-param name="date" select="string(pubDate)"/>
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by jbenoit on 30 Oct, 2014 05:23 PM

    jbenoit's Avatar

    Thank you for the quick follow up. I modified the code with the "string(pubDate)" and no change. I'm wondering if I have gummed something up…

  5. 5 Posted by Ryan Griffith on 30 Oct, 2014 05:30 PM

    Ryan Griffith's Avatar

    Hm, definitely interesting. Are you using the latest version of the format-date XSLT Format by chance?

    Perhaps try using the format-date-string template instead:

    <xsl:call-template name="format-date-string">
        <xsl:with-param name="date" select="pubDate"/>
        <xsl:with-param name="mask">mmm ddd d</xsl:with-param>
    </xsl:call-template>
    

    Note: it looks like the template already casts the element to a string, so I think it is safe to drop that suggestion.

    Please let me know if you have any questions.

    Thanks!

  6. 6 Posted by jbenoit on 30 Oct, 2014 05:59 PM

    jbenoit's Avatar

    That worked! Thank you very much!

  7. jbenoit closed this discussion on 30 Oct, 2014 05:59 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM