While looking at this node, I thought to myself instead of trying to parse HTML dumped from lynx (possibly error-prone plus requires an external program,) why not use the RDF feed that slashdot provides at http://slashdot.org/slashdot.rdf? And as you would then be converting from one XML DTD to another, isn't this a perfect opportunity to use XSLT? Here's what I came up with:
#!/usr/bin/perl -w
use strict;
#
# A script to convert slashdot headlines to VXML.
# (C) 2001, Jaldhar Vyas
# Licensed under the Crowley Public License ("Do what thou wilt
# shall be the whole of the license.")
#
use LWP::Simple qw(get);
use XML::LibXML;
use XML::LibXSLT;
my $content = get('http://slashdot.org/slashdot.rdf');
unless (defined ($content)) # undef means something went wrong.
{
$content = <<'-EOT-';
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<item>
<title>Failed to retrieve headlines</title>
</item>
</rdf:RDF>
-EOT-
}
my $xml = XML::LibXML->new();
my $xslt = XML::LibXSLT->new()->parse_stylesheet($xml->parse_fh(*DATA)
+);
print "Content-type: text/xml\n\n" ,
$xslt->output_string($xslt->transform($xml->parse_string($content)))
+;
__DATA__
<?xml version="1.0"?>
<xsl:stylesheet xmlns=""
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rss="http://my.netscape.com/rdf/simple/0.9/">
<xsl:namespace-alias stylesheet-prefix="#default" result-prefix='rss
+' />
<xsl:output method="html" media-type="text/xml" indent="yes" />
<xsl:template match="rdf:RDF">
<vxml version='2.0'>
<form><block>
<xsl:apply-templates select="rss:item/rss:title" />
</block></form>
</vxml>
</xsl:template>
<xsl:template match="rss:title">
<xsl:value-of select="." /><xsl:text>.
</xsl:text>
</xsl:template>
</xsl:stylesheet>
A problem I ran into was due to XML namespaces. RDF is built on the older RSS spec and most of the tags are actually declared in the RSS namespace. Other than that it was fairly simple and should be a lot more maintainable in the long run.