Joe Beda's EightyPercent.net

Sun, 9 Mar 2003

The three B's11:08:53 AM

Rachel and I hit the Boise trifecta last night: Bowling, Bud Light and Butt Rock. All Van Halen all the time.

Stupid XSLT tricks11:00:02 AM

In talking with Jorge about how he does his site, he pointed me to his XSLT source. I'll let him post links to those on his own site ifi he wants to. However, he knows some advanced moves that I haven't discovered yet. Here is one of them: http://www.w3.org/TR/xslt#dt-attribute-value-template. This is a huge keystroke saver. It lets me go from this:

<A class="ArchiveLink">
    <xsl:attribute name="href">
        <xsl:value-of select="$SiteRoot"/><xsl:value-of 
            select="user:FormatArchiveLink(LocalPubDate)"/>#a<xsl:value-of 
            select="BlogEntryNumber"/>
    </xsl:attribute>
    <!--....-->
</A>

To this:

<A class="ArchiveLink" href="{$SiteRoot}{user:FormatArchiveLink(LocalPubDate)}#a{BlogentryNumber}">
    <!--....-->
</A>

Very cool!

More on RSS dates10:26:04 AM

Jorge Curioso posted on his blog some comments around dealing with dates in RSS and the .Net framework. I figure I'd put some more detail up here on how I handle dates in JoeBlogger.

  1. I store both GMT and local time when creating an entry. I should probably keep a "last updated" time too but I haven't done that yet. From these two you can either output absolute time (for RSS) or you can output local time so that everyone can know your state of mind when you made that post. It makes a difference if you are posting at 5 am in the morning vs. 10 am.
  2. I used C# to do all of my date processing. Here is the code for my properites for these things:
            // The PubDate here is in universal time -- UTC
            [XmlIgnore]
            public DateTime PubDate;
    
            [XmlElement(ElementName="PubDate")]
            public string PubDateString 
            {
                get
                {
                    return PubDate.ToString("r");
                }
                set
                {
                    PubDate = DateTime.ParseExact(value,"r", null);
                }
            }
    
            [XmlIgnore]
            public DateTime LocalPubDate;
    
            [XmlElement(ElementName="LocalPubDate")]
            public string LocalPubDateString 
            {
                get
                {
                    return LocalPubDate.ToString("ddd, dd MMM yyyy HH':'mm':'ss");
                }
                set
                {
                    try
                    {
                        LocalPubDate = DateTime.ParseExact(value,"ddd, dd MMM yyyy HH':'mm':'ss", null);
                    }
                    catch (FormatException)
                    {
                        LocalPubDate = DateTime.MinValue;
                    }
                }
            }
    
  3. Finially, I do all of the massaging from these times into my display times in the stylesheet itself. Here is an example of a source XML document for my main page and here is my XSLT template for transforming that. If you can assume the .Net implementation of XSLT, you can use some MS specific extensions like this:
        <msxsl:script implements-prefix='user' language='CSharp'>
            <![CDATA[
            public string FormatTime(string input)
            {
                DateTime dt = DateTime.ParseExact(input, "ddd, dd MMM yyyy HH':'mm':'ss", null);
                return dt.ToString("h':'mm':'ss tt");
            }
            public string FormatArchiveLink(string input)
            {
                DateTime dt = DateTime.ParseExact(input, "ddd, dd MMM yyyy HH':'mm':'ss", null);
                return String.Format(@"/Archive/{0:D4}/{1:D2}/{2:D2}.html", dt.Date.Year, dt.Date.Month, dt.Date.Day);
            }
            ]]>
        </msxsl:script>
    
    And then use that in the output like this:
    <xsl:value-of select="user:FormatTime(LocalPubDate)"/>
    

It might be a good thing if we someone came up with a namespaced extension for RSS to speicfy local time along with GMT. Having "First published" and "Last changed" times might be interesting also. Perhaps I'll write something up at some point.

Write to Eightypercent AT Bedafamily DOT com - All opinions expressed are those of Joe Beda and not of his employer, wife or guinea pig - Copyright © 2003-2004 Joe Beda, all rights reserved