XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic...
-
Upload
caroline-linda-hines -
Category
Documents
-
view
218 -
download
2
Transcript of XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic...
XML for E-commerce IV
Helena Ahonen-Myka
In this part...
Some solutions for delivering dynamic content
Example of using XML
Solutions for delivering dynamic content CGI (e.g. Perl, PHP) Java servlets Java Server Pages (JSP) Extensible Server Pages (XSP)
CGI Server starts external programs using CGI
interface (input, output, requests) a new process is started and the program
code is loaded each time a request occurs (even when simultaneous requests)
stateless any programming languages: Perl, PHP
are popular
Perl CGI package
#!/usr/bin/perl
use CGI qv(:standard);
print header(), start_html(”Hello param($name)!”), h1(”Hello param($name)!”), end_html();
Assume: name is a form field:
PHP#!/usr/local/bin/php
<html title= <? echo ”Hello $name !”; ?> > <body> <?php echo ”Hello $name !”; ?> </body></html>
Java servlets
Java programs that run on a web server and build web pages
each request handled by a thread (light) only one copy of the code (for
simultaneous requests) maintaining a state easier
public class helloWWW extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(”text/html”); printWriter out = response.getWriter(); out.println(”<!DOCTYPE HTML PUBLIC …> \n” + ”<html> \n” + ”<head><title>Hello” + request.getParameter(”name”) + ”!</title>” + ”</head>\n” + ”<body>\n” + ”<h1>Hello” + request.getParameter(”name”) + ”!</ </h1>\n” + ”</body></html>”);}}
Java Server Pages (JSP)
static HTML + dynamic parts within the code, e.g. <% ...scripting %> JSP pages are converted to Java
servlets (the first time the page is requested) and the servlet is compiled
<!DOCTYPE HTML PUBLIC …> <html> <head><title>Hello <% request.getParameter(”name”) %></title> </head> <body> <h1>Hello <% request.getParameter(”name”) %></h1> </body></html>
Extensible Server Pages (XSP)
XML pages with embedded logic processing can be done before the
styling and presentation is done therefore the result can easily be used in
further processing (cf. JSP is used to generate output)
and the logic is separated from presentation
<?xml version=”1.0” ?><?xml-stylesheet href=”myStylesheet.xsl” type=”text/xsl”?>
<xsp:page language=”java” xmlns:xsp= ”http://www.apache.org/1999/XSP/Core”>
<xsp:logic> private static int numHits = 0; private synchronized int getNumHits() { return ++numHits;}</xsp:logic>
<page><title>Hit Counter</title> <p>I’ve been requested <xsp:expr>getNumHits() </xsp:expr> times.</p></page></xsp:page>
Example
The Foobar Public Library mytechbooks.com customers of mytechbooks.com
The Foobar Public Library
suppliers can add new books that they are sending to the library
an HTML page with a form:
<form method=”POST” action=”http://www.foo.com/cgi/addBook.pl”>… Title:<input type=”text” name=”title” size=”20”> Publisher:<input type=”text” name=”publ” size=”20”>…</form>
addBooks.pl
a Perl CGI script: reads the data received from the user and produces XML data
each new entry is appended to an existing file
Writing an XML file
$bookFile = ”/home/foobar/books/books.txt”;
use CGI;$query = new CGI;
$title = $query->param(’title’);$publisher = $query->(’publ’);…
if (open(FILE, ”>>” . $bookFile)) { print FILE ”<book subject=\”” . $subject . ”\”>\n”; print FILE ” <title><![CDATA[” . $title . ”]]></title>\n; … print FILE ”</book>\n\n”;
Notes: Only an XML fragment is generated: no
declaration, no root element
<book subject=”Fiction”> <title><![CDATA[Second Foundation]]></title> <author><![CDATA[Isaac Asimov]]></author> <publisher><![CDATA[Bantam Books]]></publisher> <numPages>279</numPages> <saleDetails><isbn>0553293362</isbn> <price>5.59</price ></saleDetails> <description><![CDATA[After the First Foundation…]]> </description></book>
Some other portion of the library’s application periodically reads the XML data and updates the library’s catalog: this part also removes the entries from the list of new entries
providing a listing of new available books: add an XML declaration etc.
supplyBooks.pl
$bookFile = ”/home/foobar/books/books.txt”
open(FILE, $bookFILE) || die ”Could’t open $bookFile.”;
print ”Content-type: text/plain\n\n”;
print ”<?xml version=\”1.0\”?>\n”;print ”<books>\n”;
while (<FILE>) { print ”$_”; }
print ”</books>\n”;close(FILE);
mytechbooks.com
can make HTTP requests (supplyBooks.pl) and receives a list of books in XML
needs a listing of new technical books on their own Web page
wants to advertise (push technology)
Filtering the XML data
only computer-related books are to be included (Foobar Library has several other subjects as well)
subject information is an attribute of the book element
filter out all the books whose subject is not ”Computers”
computerBooks.xsl<xsl:stylesheet xmlns:”http://….” version=”1.0”>
<xsl:template match=”books”> <html><head>…</head> <body> <!-- user interface details… --> <xsl:apply-templates select=”book[@subject=’Computers’]” /> <!-- user interface details… --> </body></xsl:template></xsl:stylesheet>
XSL stylesheet: book<xsl:template match=”book”><table> <tr><td><xsl:value-of select=”title” /></td></tr> <tr><td> Author: <xsl:value-of select=”author” /><br /> Publisher: <xsl:value-of select=”publisher” /><br /> Pages: <xsl:value-of select=”numPages” /><br /> Price: <xsl:value-of select=”saleDetails/price” /><br /> <br /> <xsl:element name=”a”> <xsl:attribute name=”href”> /servlets/BuyBookServlet?isbn= <xsl:value-of select=”saleDetails/isbn” /> </xsl:attribute></xsl:element></td></tr></table></xsl:template>
XSLT from a servletimport ...public class ListBooksServlet extends HttpServlet { private static final String hostname=”foobar.com”; private static final int portNumber = 80; private static final String file =”/cgi/supplyBooks.pl”; private static final String stylesheet = ”/home/…/computerBooks.xsl”;
public void service(HttpServletRequest req, HttpServletResponse res) throws … { res.setContentType(”text/html”); URL getBooksURL = new URL(”http”,hostname,portNumber,file); InputStream in = getBooksURL.openStream(); // transform XML into HTML }}
Notes:
Servlet requests the Foobar Public Library’s application (supplyBooks.pl) through an HTTP request, and gets the XML response in an InputStream
this stream is used as a parameter to the XSLT processor, as well as the XSL stylesheet defined as a constant in the servlet
Invoking transformations
There is current no general API that specifies how XSLT transformations can occur programmatically: each processor vendor should have classes that allow a transformation to be invoked from Java code
Invoking transformations
Apache Xalan: XSLTProcessor class in the org.apache.xalan.xslt package
parameters: the XML file to process, the stylesheet to apply, and output stream (servlet output stream can be used)
…URL getBooksURL = new URL (”http”, hostname, portNumber, file);InputStream in = getBooksURL.openStream();
try { XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
processor.process(new XSLTInputSource(in), new XSLTInputSource ( new FileInputStream(stylesheet)), new XSLTResultTarget( res.getOutputStream())); } catch (Exception e) {…}}}
Push vs. pull
Pull: the user writes an URL or follows a link, or an application makes an HTTP request
Push: data is delivered to the user without the user’s active request; e.g. which new items are available, special offers...
Personalized start pages and RSS
Personalized start pages: e.g. Netscape’s My Netscape and Yahoo’s My Yahoo
Rich Site Summary (RSS): an XML application, which defines channels
a channel provides quick info about e.g. a company’s products (for display in a portal-style framework)
RSS channel
title of the channel description of the channel image or logo items, each item may contain title,
description and hyperlink
… in our example
mytechbooks.com would like to create an RSS channel that provides new book listings: interested clients can jump directly to buying an item
a channel is registered with Netscape Netcenter
Netcenter will automatically update RSS channel content
<?xml version=”1.0”><!DOCTYPE rss PUBLIC ”-//Netscape Communications//DTD RSS 0.91//EN” ”http://my.netscape.com/publish/formats/rss-0.91.dtd”>
<rss version=”0.91”> <channel> <title>mytechbooks.com New Listings</title> <link>http://www.newInstance.com/javaxml/techbooks </link> <description>Your online source for technical material, computers, and computing books!</description> <language>en-us</language> <image>…</image> <item>…</item> <item>…</item> </channel></rss>
<item> <title>Java Servlet Programming</title> <link> http://mytechbooks.com/buy.xsp?isbn=156592391X </link> <description> This book is a superb introduction to Java servlets and their various communications mechanisms. </description></item>
Transformation into RSS
an XSLT transformation can transform the book elements to item elements:
book -> item title -> title saleDetails/isbn -> link (isbn as param.) description -> description
Validating the RSS channel
http://my.netscape.com/publish/help/ validate.tmpl
there are some constraints that cannot be enforced by the DTD
RSS channel can be a servlet, CGI script, a static file...
Registering the channel
The channel is published to Netcenter (or other service provider)
http://my.netscape.com/publish once the valid channel is accepted,
instructions how to add the channel to a web page is sent by email
Use
button or link is added to the mytechbooks.com web page: ”Add this site to My Netscape”
inclusion in the Netscape Open Directory
Not restricted to Netscape
The same RSS format can (and is) used in different applications
format is simple and generic: suitable for many purposes