Program Yahoo! with XSLT

Transform Yahoo! Search Web Services responses into HTML with an XSLT stylesheet.

The Extensible Stylesheet Language (XSL) is a tag-based template system that can transform any XML document into any other text format, including other flavors of XML or, more commonly, HTML. Like a scripting language, XSL defines what data should go where on a page. You’ll still need to use a scripting language to perform the transformation from XML into HTML, and the entire process is encapsulated in the term XSL transformations or XSLT.

At first glance, an XSL stylesheet looks a lot like an HTML page, and it often contains HTML tags. But unlike HTML, each stylesheet must be valid XML and must contain a number of tags that describe how the XML should be processed. While an HTML document can have a few unclosed tags here and there and still display a web page, XSL is very strict and will fail if it’s not properly formed.

Responses from the Yahoo! Search Web Services are in XML and can be transformed directly into an HTML page with a bit of XSL.

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();

Running the Hack

To run the code, upload both files to a web server and bring up the script in a browser, adding a search term like this:

http://example.com/yahoo_xslt.cgi?insert query

So searching for the phrase "Elements of Style" looks like this:

http://example.com/yahoo_xslt.cgi?Elements%20of%20Style

Figure 4-9 shows the results of the script in a browser, with the XML formatted as HTML with the stylesheet.

Hacking the Hack

This example was written with Perl, but you could make Yahoo! API requests with your favorite development environment. If you were to re-create this example in PHP, Python, or Visual Basic, the XSL stylesheet would stay the same and only the script that makes requests and processes the transformation would change.

Yahoo! Search API response transformed with XSLT

Figure 4-9. Yahoo! Search API response transformed with XSLT

Get Yahoo! Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.