O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Yahoo! Hacks
By Paul Bausch
October 2005
More Info

HACK
#73
Program Yahoo! with XSLT
Transform Yahoo! Search Web Services responses into HTML with an XSLT stylesheet
The Code
[Discuss (0) | Link to this hack]

The Code

Each stylesheet is organized into one or more templates that define how data from the source XML document should be arranged. The templates within a stylesheet contain a mix of XSL processing tags and HTML.

To try a transformation out, first create the stylesheet. Save the following XSL to a text file called yahoo_search.xsl:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Results</title>
</head>
<body>

	<h1>Search Results</h1>
		<!-- Begin Search Results -->
	<ol>
	<xsl:apply-templates select="//ResultSet/Result"/>
	</ol>
		<!-- End Search Results -->
<p>Powered by Yahoo!</p>

</body>
</html>
</xsl:template>

<xsl:template match="ResultSet/Result">
	<li style="margin-bottom:10px;">

			<a> 
			<xsl:attribute name="href">
				<xsl:value-of select="ClickUrl"/>
			</xsl:attribute>
			<xsl:value-of select="Title"/>
			</a><br />
			<xsl:value-of select="Summary"/><br />
			<xsl:value-of select="Url"/>

</li>
</xsl:template>

</xsl:stylesheet>

In addition to the stylesheet, you'll need a scripting language to make the request and perform the transformation. Every development environment has XSLT tools you can use; this example uses Perl. As with most of the Perl examples in this book, you'll need the component LWP::Simple to make Yahoo! API requests. And to work with XSL, you'll need the XML::XSLT module.

This script accepts a search query term, assembles the Yahoo! API request URL, and fetches the response. Then it uses the XML::XSLT module to apply the stylesheet to the XML response and it prints the results. To create the script, save the following code to a file called yahoo_xslt.cgi:

#!/usr/bin/perl
# yahoo_xslt.cgi
# Accepts a search term and shows the top results.
# Usage: yahoo_xslt.cgi?<query>
#
# You can create an AppID, and read the full documentation
# for Yahoo! Web Services at http://developer.yahoo.net/

use strict;
use XML::XSLT;
use LWP::Simple;

# Set the XSL stylesheet
my $xslfile = "yahoo_search.xsl";

# Set your unique Yahoo! Application ID
my $appID = "insert Application ID";

# Grab the incoming search query
my $query = join(' ', @ARGV);
unless ($query) {
  print "Content-type: text/plain\n\n";
  print "Usage: yahoo_xslt.cgi?query";

exit;
}

# Construct a Yahoo! Search Query with only required options
my $language = "en";
my $req_url = "http://api.search.yahoo.com/";
   $req_url .= "WebSearchService/V1/webSearch?";
   $req_url .= "appid=$appID";
   $req_url .= "&query=$query";
   $req_url .= "&language=$language";

# Make the request
my $yahoo_response = get($req_url);

# Transform the response
my $xslt = XML::XSLT->new ($xslfile, warnings => 1);
$xslt->transform ($yahoo_response);

# Print the transformation
print "Content-Type: text/xml\n\n";
print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ',
	  '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
print $xslt->toString;

# Clean up
$xslt->dispose();


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.