Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Sounds like a job for XSLT (tan-tan-tan-tan-tanaaa!) by jaldhar
in thread Slashdot over the phone by beretboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found