- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
At this point, we've built a Perl script that sends a request to the Amazon Electronic Commerce Service and gets information on the first 10 items on your wish list. Now we want to turn that script into a Movable Type plug-in that displays a random item on that list when the pages built or dynamically displayed. To do that, will need to work out how to select one random item, and then convert the script into a plug-in.
First, the logic.
So far, we've displayed all of the first 10 items on the list. To display a single item, we need to access the items as an array:
#!/usr/local/bin/perl use SOAP::Lite; my $lookup = SOAP::Lite->uri('http://webservices.amazon.com/AWSECommerceService/2005-07-26')->proxy('http://soap.amazon.com/onca/soap?Service=AWSECommerceService'); my $SubscriptionIdElement = SOAP::Data->name('SubscriptionId') ->value('0NQJY4BXVZP3SFWWPZG2'); my $AssociateTagElement = SOAP::Data->name('AssociateTag') ->value('thevanguardsc-20'); my $ListIdElement = SOAP::Data->name('ListId') ->value('4M6WEGGRFLZO'); my $ListTypeElement = SOAP::Data->name('ListType') ->value('WishList'); my $ResponseGroupElement = SOAP::Data->name('ResponseGroup') ->value('Medium'); my $RequestElementType = SOAP::Data->value($ListIdElement, $ListTypeElement, $ResponseGroupElement); my $RequestElement = SOAP::Data->name('Request') ->value($RequestElementType); my $ListLookupRequest = SOAP::Data->value($SubscriptionIdElement, $RequestElement); my $response = $lookup->ListLookup($ListLookupRequest); print "RequestProcessingTime = "; print $response->dataof('//RequestProcessingTime')->value; print "\n"; @itemarray = $response->valueof('//Item'); my $item = $itemarray[7]; print "<p><a href='" . $item->{DetailPageURL} . "'>"; print "<img src='" . $item->{SmallImage}->{URL} . "' />"; print "</a>"; print "<br />"; print $item->{ItemAttributes}->{Title}; print "</p>\n";
Notice that I've also changed the output to match the HTML we'll ultimately want to display, so the resulting text looks something like this:
RequestProcessingTime = 0.948960065841675<a href='http://www.amazon.com/exec/obidos/redirect?tag=ws%26link_code=sp1%26camp=2025%26creative=165953%26path=http://www.amazon.com/gp/redirect.html%253fASIN=0764549103%2526tag=ws%2526lcode=sp1%2526cID=2025%2526ccmID=165953%2526location=/o/ASIN/0764549103%25253FSubscriptionId=0NQJY4BXVZP3SFWWPZG2%252526coliid=IZD3O6ZXGY3OC%252526colid=4M6WEGGRFLZO'><img src='https://images.amazon.com/images/P/0764549103.01._SCTHUMBZZZ_.jpg' /></a><br />UML Weekend Crash Course</p>
As for which item to display, we need a number between one and the number of items on the list:
... my $response = $lookup->ListLookup($ListLookupRequest); print "RequestProcessingTime = "; print $response->dataof('//RequestProcessingTime')->value; print "\n"; $totalItems = 24; $thisItem = sprintf("%d", rand $totalItems); print "Requesting item " . $thisItem . "\n"; @itemarray = $response->valueof('//Item'); my $item = $itemarray[$thisItem]; ...
Here we're using Perl's rand
function to create a fractional number between
0 and the number of items on the list, and then using sprintf
to
turn it into an integer.
Note that I'm specifying the number of items on the list. We could do a list look up and find out the current size of the list, it's not a good idea for performance reasons. We're already going to significantly slow down the rebuild process by requiring a Web service call for each page. Two calls would be ridiculous. In fact, according to Amazon's terms of service, we need to limit our activity to a maximum of one call per second:
... print "<br />"; print $item->{ItemAttributes}->{Title}; print "</p>\n"; sleep(1); print "Ready for the next one.\n";
Now we need to make sure the target item is available.
Each page includes 10 items, so we need to
add the ProductPage
parameter to our request:
#!/usr/local/bin/perl use lib qw(/home/nicholaschase/www/cgi-bin/lib/5.6.1/i386-linux /home/nicholaschase/www/cgi-bin/lib/site_perl/5.6.1); use SOAP::Lite; $totalItems = 24; $thisItem = sprintf("%.0f", rand $totalItems); print "Requesting item " . $thisItem . "\n"; $rawProductPage = ($thisItem/10); $productPage = sprintf("%.0f", $rawProductPage); if ($rawProductPage - $productPage > 0) { $productPage = $productPage + 1; } my $lookup = SOAP::Lite->uri('http://webservices.amazon.com/AWSECommerceService/2005-07-26')->proxy('http://soap.amazon.com/onca/soap?Service=AWSECommerceService'); my $SubscriptionIdElement = SOAP::Data->name('SubscriptionId') ->value('0NQJY4BXVZP3SFWWPZG2'); my $AssociateTagElement = SOAP::Data->name('AssociateTag') ->value('thevanguardsc-20'); my $ListIdElement = SOAP::Data->name('ListId') ->value('4M6WEGGRFLZO'); my $ListTypeElement = SOAP::Data->name('ListType') ->value('WishList'); my $ResponseGroupElement = SOAP::Data->name('ResponseGroup') ->value('Medium'); my $ProductPageElement = SOAP::Data->name('ProductPage') ->value($productPage); my $RequestElementType = SOAP::Data->value($ListIdElement, $ListTypeElement, $ResponseGroupElement, $ProductPageElement); my $RequestElement = SOAP::Data->name('Request') ->value($RequestElementType); my $ListLookupRequest = SOAP::Data->value($SubscriptionIdElement, $RequestElement); my $response = $lookup->ListLookup($ListLookupRequest); print "RequestProcessingTime = "; print $response->dataof('//RequestProcessingTime')->value; print "\n"; print "Product page =".$productPage."\n"; $thisArrayItem = $thisItem - (($productPage-1) * 10) -1; print "Requesting array item " . $thisArrayItem . "\n"; @itemarray = $response->valueof('//Item'); my $item = $itemarray[$thisArrayItem]; ...
Now when we run the script, will get a different item each time:
[nicholaschase@spinel cgi-bin]$ perl amz2.cgi Requesting item 3 RequestProcessingTime = 3.87524199485779 Product page = 1 Requesting array item 2Ready for the next one. [nicholaschase@spinel cgi-bin]$ perl amz2.cgi Requesting item 17 RequestProcessingTime = 0.854296207427979 Product page = 2 Requesting array item 6
Evita: The Complete Motion Picture Music SoundtrackReady for the next one.
The Pleasure of Finding Things Out and the Meaning of It All
At this point, we're finally ready to turn this thing into a
plug-in. To do that, copy the Perl
script into your Movable Type plugins
directory and strip out the debugging information.
Next, add a package declaration and the template context:
#!/usr/local/bin/perl package MT::Plugin::AmazonWishList; use SOAP::Lite; use MT::Template::Context; MT::Template::Context->add_tag(AmazonWishList => \&wishlist); sub wishlist() { $totalItems = 24; $thisItem = sprintf("%.0f", rand $totalItems); $rawProductPage = ($thisItem/10); $productPage = sprintf("%.0f", $rawProductPage); ... $thisArrayItem = $thisItem - (($productPage-1) * 10) -1; @itemarray = $response->valueof('//Item'); my $item = $itemarray[$thisArrayItem]; $returnString = "<p><a href='" . $item->{DetailPageURL} . "'>"; $returnString = $returnString . "<img border='0' src='" . $item->{SmallIma ge}->{URL} . "' />"; $returnString = $returnString . "<br />"; $returnString = $returnString . $item->{ItemAttributes}->{Title}; $returnString = $returnString . "</a>"; $returnString = $returnString . "</p>\n"; sleep(1); return "Results are ".$returnString; }
What we're actually doing here is associating a tag, which we'll add to the template, with the Perl subroutine. As such, we've also made a subroutine out of our code, returning the final string instead of directly printing it.
Now we need to add the tag to our page. In your template,
add the MTAmazonWishList
tag:
This is a test. It is only a test. <MTAmazonWishList />
Now first of all, I've obviously created a simple template for
testing here. Second, notice that while we mapped the subroutine
to the AmazonWishList
tag, when we actually use it it
becomes the MTAmazonWishList
tag.
Finally, we rebuild the page, and see the results.
Our final task is to make it possible to pass in user-configurable information, such as the list ID. We can do that by adding attributes to the tag:
This is a test. It is only a test. <MTAmazonWishList totalItems="24" token="0NQJY4BXVZP3SFWWPZG2" listId="4M6WEGGRFLZO" associateTag="thevanguardsc-20" />
We can then read the attributes from the plug-in, which receives them as an argument:
#!/usr/local/bin/perl package MT::Plugin::AmazonWishList; use SOAP::Lite; use MT::Template::Context; MT::Template::Context->add_tag(AmazonWishList => \&wishlist); sub wishlist() { my $context = shift; my $attributes = shift; $totalItems = $attributes->{totalItems}; $token = $attributes->{token}; $associateTag = $attributes->{associateTag}; $listId = $attributes->{listId} $thisItem = sprintf("%.0f", rand $totalItems); $rawProductPage = ($thisItem/10); $productPage = sprintf("%.0f", $rawProductPage); if ($rawProductPage - $productPage > 0) { $productPage = $productPage + 1; } my $lookup = SOAP::Lite->uri('http://webservices.amazon.com/AWSECommerceService/ 2005-07-26')->proxy('http://soap.amazon.com/onca/soap?Service=AWSECommerceServic e'); my $SubscriptionIdElement = SOAP::Data->name('SubscriptionId') ->value($token); my $AssociateTagElement = SOAP::Data->name('AssociateTag') ->value($associateTag); my $ListIdElement = SOAP::Data->name('ListId') ->value($listId); my $ListTypeElement = SOAP::Data->name('ListType') ->value('WishList'); ...
You can also use these attributes to set styles for the final output, or to determine other features such as surrounding text. And now, because the plugin's been generalized, you can simply install the Perl script on any Movable Type installation and go from there.