The XML Web
Author: Darshan
Singh,
Managing
Editor, perfectxml.com
darshan@perfectxml.com
April
14, 2002
Updated on April 22, 2002: Click here to see an example of integrating Microsoft Office XP Smart Tags with the Google Web API.
Contents:
Abstract
In this
short article, I'll talk about how the "HTML Web" is moving towards
the "XML Web".
Amazon.com Associates XML
Interface
Recently, Amazon.com,
Inc. announced the XML interface for their Associates. This HTTP based XML
interface allows associates to build targeted, customized Amazon placements on
their Web site. For instance, an associate can send an HTTP request to search
for various product lines (books, music, etc.) and pass the subject keywords as
part of the URL query, and get back the
results in XML (instead of HTML).
First,
let's look at the URL syntax and then we'll look at an example:
http://rcm.amazon.come/cm?t=[Associates
ID]&l=st1&search=[keywords]&mode=[product
line]&p=102&o=1&f=xml
The above
HTTP URL accepts associate ID, product line, search keywords, and few other
required parameters.
perfectxml.com
is an Amazon associate for quite some time now. Let's try the above URL using
the PerfectXML associate ID. Let's search for books on MSXML:
http://rcm.amazon.com/e/cm?t=theultimxmlso-20&l=st1&search=msxml&mode=books&p=102&o=1&f=xml
Browse to
the above URL and you'll see the results returned in the XML format. The
associates can now very easily parse the returned XML and present the results
either by applying a custom XSL Transformation or use XML processing APIs (such
as DOM or SAX) to get to the returned data elements.
Amazon also
offers the ability to search for best selling products in a particular browse
category (VHS, DVD, Toys, Baby, etc.) and get the results back in XML format.
Let's search for best selling baby toys on Amazon.com:
http://rcm.amazon.com/e/cm?t=theultimxmlso-20&l=bn1&browse=540988&mode=toys&p=102&o=1&f=xml
The browse
category of 540988 refers to the Baby products
and mode=toys specify the product
line.
Using MSXML ServerXMLHTTP and XSLT
Let's look
at a very simple example to use the Amazon.com Associates XML Interface. We'll
use MSXML ServerXMLHTTP
class to send the HTTP GET request to
one of the above URL (the baby toys on Amazon.com), get back the XML, and apply
XSL Transformation to pretty print the results.
View Demo
Let's begin
by looking at the ASP code that use MSXML 3.0 ServerXMLHTTP class, sends the
HTTP GET request, gets the XML
response back, loads the XSL Transformation file using MSXML DOM, applies the
transformation and sends the result HTML to the client:
AmazonToys.asp
|
<%
Option
Explicit
Dim
ObjSXH, ObjXML, ObjXSL
'Using
MSXML 3.0 ServerXMLHTTP Class
Set
ObjSXH = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
'GET
Request
ObjSXH.open
_
"GET",
_
"http://rcm.amazon.com/e/cm?t=theultimxmlso-20&l=bn1&browse=540988&mode=toys&p=102&o=1&f=xml",
_
False
'Send
the request
ObjSXH.send ""
'If
the request succeeded
If
ObjSXH.status = 200 Then
'returned XML as a DOMDocument
Set
ObjXML = ObjSXH.responseXML
'Load the XSLT stylesheet
Set ObjXSL =
Server.CreateObject("Msxml2.DOMDocument.3.0")
ObjXSL.load Server.MapPath("amazontoys.xsl")
'Apply XSL Transformation and write the
HTML result
Response.Write
(ObjXML.transformNode(ObjXSL))
Else
Response.Write "An error occurred.
Please try later."
End
If
%>
|
And here is
the XSL Transformation file used in the above ASP page:
AmazonToys.xsl
|
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"
indent="yes"/>
<xsl:template match="/">
<table align="center" width="600"
cellpadding="3" cellspacing="1"
bgcolor="#333333">
<xsl:for-each select="//product">
<tr>
<td bgcolor="#EEEEEE"
width="300" valign="top">
<b><xsl:value-of
select="title" /></b><br
/>
<font color="gray">
Author: <xsl:value-of
select="author" /> <br />
Sales Rank:
<b><xsl:value-of select="ranking" /></b>
</font>
</td>
<td bgcolor="#FFFFFF"
width="300" valign="top"
align="center">
<a href="{tagged_url}"><img border="0"
src="{image}" /><br
/>Buy</a>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
|
Click here to view the
output of ASP+XSLT code.
Notes
-
Visit http://forums.prospero.com/am-assocdevxml
if you have any questions and discuss it on Amazon discussion forums.
-
Visit http://cybaea.com/Associates.html
to download the package that helps in creation of complex, highly targeted ads
from the raw Amazon XML interface.
-
The current
limitations include:
§
A single XML
query can only generate results for one product line at a time, and
§
The result set
is currently restricted to the maximum of 15 items.
-
Send any
questions or comments to associates-vendor@amazon.com.
-
Amazon
recommends caching the XML results.
Section Summary
During one of the
weekend in August 2001, I wrote an ASP.NET Web service to get the Amazon and
B&N sales rank and price information (SalesRankNPrice
Web service) – it was not fun trying out various regular expressions to
find the "best" one that always
select the correct values from the HTML that I got from the book store Web
sites. I am glad that the HTML Web is now moving towards the XML, and it
certainly is good news for us (the developers), we can use our imagination and
build new intelligent applications around this XML Web. Drop a line at mailto:darshan@perfectxml.com and let
me know your thoughts on this!
Google Web APIs
On April 11, 2002, Google made a public release of the Google Web APIs Beta 2 – SOAP based API that currently supports three major
operations:
1.
Search the Web,
2.
Request a
spell correction, and
3.
Retrieve the
cached Web page.
The Web APIs home
page (http://www.google.com/apis/)
allows downloading the developer's kit that contains:
-
A WSDL file
that formally describes the service.
-
API Reference
documentation (HTML format), and Readme.txt.
-
A custom Java
library that provides a convenience wrapper for Java programmers.
-
Documentation
for the above example Java library.
-
Example SOAP
messages and responses.
-
Sample C# .NET
code application that uses the above Web service.
-
The Readme.txt
file also contains the SOAP::Lite 0.52 example.
In order to use
Google Web APIs you first must register
with Google to receive an authentication key. Go ahead and download the kit, register
and obtain your license key (free); and begin using the Google Web service.
It is required to pass the license number (authentication) key with each Web
method call you make to the Google Web service.
Click here to view some
example of applications that you can create using the Google Web API.
Using Microsoft SOAP Toolkit 2.0
Let's look at an ASP
VBScript example to search for the word "certification" on the Web
site www.perfectxml.com using the Google Web API.
We assume that you
have GoogleSearch.wsdl (from the download kit) saved
under the same directory as the following ASP page:
GoogleSearch.asp
|
<%
Option
Explicit
Dim
ObjSOAPClient, ResultNodeList
Dim
MyGoogleKey, SearchStr
Dim
iIndex, ApproxResultCount
MyGoogleKey = "***WRITE_GOOGLE_AUTH_KEY_HERE***"
SearchStr =
"site:www.perfectxml.com certification"
Set
ObjSOAPClient = Server.CreateObject("MSSOAP.SoapClient.1")
'Initialize
the SOAPClient with the WSDL
ObjSOAPClient.mssoapinit Server.MapPath("GoogleSearch.wsdl")
'Call
doGoogleSearch Web method
Set
ResultNodeList = ObjSOAPClient.doGoogleSearch
_
(MyGoogleKey, SearchStr, 0, 1, False,
"", False, "", "", "")
For
iIndex = 0 To ResultNodeList.length - 1
If ResultNodeList.Item(iIndex).nodeName =
"estimatedTotalResultsCount"
Then
ApproxResultCount =
ResultNodeList.Item(iIndex).nodeTypedValue
Exit For
End If
Next
Response.Write
ApproxResultCount
%>
|
The above code uses
Microsoft SOAP Toolkit high-level API to call the Web service method doGoogleSearch, which returns the
complex XML data structure. The resultant XML can be processed either using SOAP
Message Object (SMO) or using MSXML. The above code uses MSXML IXMLDOMNodeList type variable to store
the XML result returned by the doGoogleSearch
Web method. The code then iterates over the IXMLDOMNodeList
looking for the estimatedTotalResultsCount,
and returns the node value to the client using Response.Write. The estimatedTotalResultsCount
currently is just 7, as Google has not yet indexed the just announced IBM XML Certification Resource Center (http://www.perfectxml.com/Certify)
on PerfectXML.
Notes
-
Visit Google Web API FAQ (http://www.google.com/apis/api_faq.html)
to see a list of frequently asked questions.
-
Join the
Google Web API discussions at http://groups.google.com/groups?hl=en&group=google.public.web-apis.
-
Initially, Google shipped the
WSDL file as part of the download ZIP file; now it's also available on Google Web site at http://api.google.com/GoogleSearch.wsdl.
-
If you are using
SOAP::Lite to access the Google Web
service, use the SOAP::Lite version 0.52 or higher.
-
Click
here to view the JSP code that uses Google Web API.
-
Click here
for some details on how to use the API with .NET.
-
Click here
to view the gSOAP example that uses the Google Web API.
-
The current
limit on the search API: maximum of 1000 queries per day, and each query
returns maximum of 10 result entries. After the 1000 queries on a same day, the
Web service returns the SOAP Fault response. Maximum number of words allowed
per query is ten (maximum of 2048 bytes). And if you wish to search only the
specific sites (using the site:
attribute), maximum of one site URL can be provided.
-
Spelling
request is also limited by ten words (2048 bytes) per query string.
-
As the Google
Web API returns complex data types, it cannot be directly used via the Office
XP Web service toolkit; the workaround would be to use MSXML. See the article Handling
Complex SOAP Data Types in XML Web Services on MSDN for more information on
this.
-
Send any
questions or comments to api-support@google.com.
-
Try the Google Web API interface using the SOAP Test Client:
Browse to the SOAP Test Client and enter following values in the form:
Post URL: http://api.google.com/search/beta2
Content-Type: text/xml; charset=utf-8
SOAPAction: urn:GoogleSearchAction
SOAP Request:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1:doGoogleSearch xmlns:ns1="urn:GoogleSearch"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<key xsi:type="xsd:string">****ENTER YOUR KEY HERE****</key>
<q xsi:type="xsd:string">PerfectXML</q>
<start xsi:type="xsd:int">0</start>
<maxResults xsi:type="xsd:int">10</maxResults>
<filter xsi:type="xsd:boolean">true</filter>
<restrict xsi:type="xsd:string"></restrict>
<safeSearch xsi:type="xsd:boolean">false</safeSearch>
<lr xsi:type="xsd:string"></lr>
<ie xsi:type="xsd:string">latin1</ie>
<oe xsi:type="xsd:string">latin1</oe>
</ns1:doGoogleSearch>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Remember to update the above SOAP request and put your Google license (authentication) key within in the <key> tag.
Summary
Amazon.com, the
biggest online bookstore on the earth, recently announced the XML integration
API to allow their associates to build targeted, customized Amazon placements.
Google.com,
the world's best search engine, recently announced the SOAP-based Web API to
search the Web, request a spell
correction, or to request a cached Web page.
In this article, we
took the above two examples to illustrate how the HTML Web is moving
towards the "XML Web" – and it's just a beginning. We'll see many such examples
in the coming days.
Related Links