xml for e-commerce iv helena ahonen-myka. in this part... n some solutions for delivering dynamic...

39
XML for E-commerce IV Helena Ahonen-Myka

Upload: caroline-linda-hines

Post on 25-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

XML for E-commerce IV

Helena Ahonen-Myka

Page 2: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

In this part...

Some solutions for delivering dynamic content

Example of using XML

Page 3: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

Solutions for delivering dynamic content CGI (e.g. Perl, PHP) Java servlets Java Server Pages (JSP) Extensible Server Pages (XSP)

Page 4: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 5: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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:

Page 6: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

PHP#!/usr/local/bin/php

<html title= <? echo ”Hello $name !”; ?> > <body> <?php echo ”Hello $name !”; ?> </body></html>

Page 7: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 8: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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>”);}}

Page 9: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 10: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

<!DOCTYPE HTML PUBLIC …> <html> <head><title>Hello <% request.getParameter(”name”) %></title> </head> <body> <h1>Hello <% request.getParameter(”name”) %></h1> </body></html>

Page 11: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 12: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

<?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>

Page 13: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

Example

The Foobar Public Library mytechbooks.com customers of mytechbooks.com

Page 14: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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>

Page 15: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 16: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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”;

Page 17: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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>

Page 18: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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.

Page 19: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 20: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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)

Page 21: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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”

Page 22: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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>

Page 23: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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>

Page 24: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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 }}

Page 25: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 26: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 27: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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)

Page 28: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

…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) {…}}}

Page 29: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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...

Page 30: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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)

Page 31: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

RSS channel

title of the channel description of the channel image or logo items, each item may contain title,

description and hyperlink

Page 32: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

… 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

Page 33: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

<?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>

Page 34: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

<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>

Page 35: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 36: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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...

Page 37: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

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

Page 38: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

Use

button or link is added to the mytechbooks.com web page: ”Add this site to My Netscape”

inclusion in the Netscape Open Directory

Page 39: XML for E-commerce IV Helena Ahonen-Myka. In this part... n Some solutions for delivering dynamic content n Example of using XML

Not restricted to Netscape

The same RSS format can (and is) used in different applications

format is simple and generic: suitable for many purposes