Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Sounds like a job for XSLT (tan-tan-tan-tan-tanaaa!)

by jaldhar (Vicar)
on Nov 28, 2001 at 13:16 UTC ( [id://128021]=note: print w/replies, xml ) Need Help??


in reply to Slashdot over the phone

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://128021]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-24 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found