开发者

Classic ASP xsl date conversion

开发者 https://www.devze.com 2023-04-09 07:28 出处:网络
I have the following code that reads a rss feed into my page, but I would like to have the pubDate convert into a more human readable date if alt all possible.

I have the following code that reads a rss feed into my page, but I would like to have the pubDate convert into a more human readable date if alt all possible.

  <?xml version="1.0" encoding="iso-8859-1"?><!-- DWXMLSource="mm_news.xml" -->
  <!DOCTYPE xsl:stylesheet]>
  <xsl:output method="html" encoding="iso-8859-1"/>
  <xsl:template match="/">
    <p class="newsList-date"><xsl:value-of select="pubDate"/></p>........

This gives me:

  Fri, 9 Sept 2011 15:21:36 GMT

but Would Like to read something like

  Friday 9 Sept 2011

Even be happy if I could simply trim off the end to just have 'Fri, 9 Sept 2011'

Also if easier can I add an extra section within the xml so i can simply enter the date like I want it so I can read it, something like below? (The开发者_如何学运维 xml is hand written not dynamically created)

    <?xml version="1.0" encoding="US-ASCII" ?> 
    <?xml-stylesheet title="XSL_formatting" type="text/xsl" href="direct.xsl"?>
    <rss version="2.0">
    <channel>....

    <item>....
    <title>.....
    <description>....
    <thedate>.....

Many Thanks


Well, the quick & dirty way would be to substitute select="pubDate" for an expression like this:

select="substring(pubDate,1,16)"

That one's dependent on the month being four letters however, and only gives you your 'fallback' result of 'Fri, 9 Sept 2011'.

If necessary, you can be a bit cleverer and remove the requirement of the month being four letters (which seems unlikely for May), by using this expression:

select="substring(pubDate,1,string-length(substring-before(pubDate,':'))-3)"

Rather than taking a fixed length of 16, it bases it on where the first : is (in the time), and subtracts 3 from that.

If you REALLY want, there's a one-line expression that can give you what you want, but it's a bit convoluted:

select="concat(normalize-space(substring('Monday    Tuesday  WednesdayThursday Friday   Saturday Sunday   ',string-length(substring-before('MonTueWedThuFriSatSun',substring(pubDate,1,3))) * 3 + 1,9)),substring(pubDate,5,string-length(substring-before(pubDate,':'))-7))"

This uses a 'lookup', to find where the day of the week exists in one string, and uses that to pick the full name from another, finally using 'normalize-space' to trim any extra spaces. Then it just concatenates it with the date part.

0

精彩评论

暂无评论...
验证码 换一张
取 消