« Water | Main | The Hello World Collection »

July 13, 2005

Monad and RSS, Part 5

First, I'll 'fess up that in a previous post I referred to the &$scriptblock notation as "eval"ing a script block, but that's wrong. Our language dev said it should just be "calling" the scriptblock. "Eval", in scripting languages, means taking a string (not a script block) and running it back through the parser (hope I got that right, Bruce!).

Anyway, in the possibly last installment of this series, I'll improve the display of the individual entries. In part 3 I talked about the file "rss.format.mshxml" that we could create. Now expand the file to contain the following:

<View>
    <Name>RSS20.Feed</Name>
    <ViewSelectedBy>
        <TypeName>System.Xml.XmlElement</TypeName>
    </ViewSelectedBy>
    <ListControl>
        <ListEntries>
            <ListEntry>
                <ListItems>
                    <ListItem>
                        <PropertyName>pubDate</PropertyName>
                        <Label>Date</Label>
                    </ListItem>
                    <ListItem>
                        <PropertyName>Title</PropertyName>
                    </ListItem>
                    <ListItem>
                        <ScriptBlock>if ($_.Description -is [System.Xml.XmlElement]) { $_.Description.get_InnerXml().SubString(0,100) } else { $_.Description.SubString(0,100) }</ScriptBlock>
                        <Label>Text</Label>
                    </ListItem>
                </ListItems>
            </ListEntry>
        </ListEntries>
    </ListControl>
</View>
<View>
    <Name>RDF.Feed</Name>
    <ViewSelectedBy>
        <TypeName>System.Xml.XmlElement</TypeName>
    </ViewSelectedBy>
    <ListControl>
        <ListEntries>
            <ListEntry>
                <ListItems>
                    <ListItem>
                        <PropertyName>dc:date</PropertyName>
                        <Label>Date</Label>
                    </ListItem>
                    <ListItem>
                        <PropertyName>Title</PropertyName>
                    </ListItem>
                    <ListItem>
                        <ScriptBlock>$_.Description.SubString(0,100)</ScriptBlock>
                        <Label>Text</Label>
                    </ListItem>
                </ListItems>
            </ListEntry>
        </ListEntries>
    </ListControl>
</View>
<View>
    <Name>Atom.Feed</Name>
    <ViewSelectedBy>
        <TypeName>System.Xml.XmlElement</TypeName>
    </ViewSelectedBy>
    <ListControl>
        <ListEntries>
            <ListEntry>
                <ListItems>
                    <ListItem>
                        <PropertyName>created</PropertyName>
                        <Label>Date</Label>
                    </ListItem>
                    <ListItem>
                        <PropertyName>Title</PropertyName>
                    </ListItem>
                    <ListItem>
                        <ScriptBlock>$_.content."#text".SubString(0,100)</ScriptBlock>
                        <Label>Text</Label>
                    </ListItem>
                </ListItems>
            </ListEntry>
        </ListEntries>
    </ListControl>
</View>

Some explanations:

  • The names of the feeds intentionally match the "feed names" (RSS20, RDF, etc) from our hash table in part 4.
  • For a list view, the property name is automatically used as the label unless you override it using the <Label> tag.
  • The script block for an RSS20 excerpt is there because I discovered some feeds send the Description as a string, others as a #cdata section. Other feeds may do this also and may need a similar script block.
  • The choice to display the first 100 characters (via the Substring() call) is completely arbitrary on my part.

Now, once you have this in place, you can just change the list in get-feeds.msh from:

$_

to

$_ | format-list -view ($feedname + ".Feed")

and each feed will show its Date/Title/Text properly.

There has actually been some discussion that the type for a System.Xml.XmlElement would be enhanced to include the namespace, so you could do this without having to specify the -View parameter to format-list, but that's still being worked out.

Posted by AdamBa at July 13, 2005 04:08 PM

Trackback Pings

TrackBack URL for this entry:
http://proudlyserving.com/cgi-bin/mt-tb.cgi/266

Comments