XML for Ecommerce IV Helena AhonenMyka In this

  • Slides: 39
Download presentation
XML for E-commerce IV Helena Ahonen-Myka

XML for E-commerce IV Helena Ahonen-Myka

In this part. . . n n Some solutions for delivering dynamic content Example

In this part. . . n n Some solutions for delivering dynamic content Example of using XML

Solutions for delivering dynamic content n n CGI (e. g. Perl, PHP) Java servlets

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

CGI n n Server starts external programs using CGI interface (input, output, requests) a

CGI n n 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 n Assume: name is a form field: #!/usr/bin/perl use CGI qv(:

Perl CGI package n Assume: name is a form field: #!/usr/bin/perl use CGI qv(: standard); print header(), start_html(”Hello param($name)!”), h 1(”Hello param($name)!”), end_html();

PHP #!/usr/local/bin/php <html title= <? echo ”Hello $name !”; ? > > <body> <?

PHP #!/usr/local/bin/php <html title= <? echo ”Hello $name !”; ? > > <body> <? php echo ”Hello $name !”; ? > </body> </html>

Java servlets n n Java programs that run on a web server and build

Java servlets n n 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 hello. WWW extends Http. Servlet { public void do. Get (Http. Servlet.

public class hello. WWW extends Http. Servlet { public void do. Get (Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { }} response. set. Content. Type(”text/html”); print. Writer out = response. get. Writer(); out. println(”<!DOCTYPE HTML PUBLIC …> n” + ”<html> n” + ”<head><title>Hello” + request. get. Parameter(”name”) + ”!</title>” + ”</head>n” + ”<body>n” + ”<h 1>Hello” + request. get. Parameter(”name”) + ”!</ </h 1>n” + ”</body></html>”);

Java Server Pages (JSP) n n n static HTML + dynamic parts within the

Java Server Pages (JSP) n n n 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. get. Parameter(”name”) %></title> </head> <body> <h

<!DOCTYPE HTML PUBLIC …> <html> <head><title>Hello <% request. get. Parameter(”name”) %></title> </head> <body> <h 1>Hello <% request. get. Parameter(”name”) %></h 1> </body> </html>

Extensible Server Pages (XSP) n n XML pages with embedded logic processing can be

Extensible Server Pages (XSP) n n 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=”my. Stylesheet. xsl” type=”text/xsl”? >

<? xml version=” 1. 0” ? > <? xml-stylesheet href=”my. Stylesheet. xsl” type=”text/xsl”? > <xsp: page language=”java” xmlns: xsp= ”http: //www. apache. org/1999/XSP/Core”> <xsp: logic> private static int num. Hits = 0; private synchronized int get. Num. Hits() { return ++num. Hits; } </xsp: logic> <page><title>Hit Counter</title> <p>I’ve been requested <xsp: expr>get. Num. Hits() </xsp: expr> times. </p></page></xsp: page>

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

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

The Foobar Public Library n n suppliers can add new books that they are

The Foobar Public Library n n 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/add. Book. pl”> … Title: <input type=”text” name=”title” size=” 20”> Publisher: <input type=”text” name=”publ” size=” 20”> … </form>

add. Books. pl n n a Perl CGI script: reads the data received from

add. Books. pl n n 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 $book. File = ”/home/foobar/books. txt”; use CGI; $query = new

Writing an XML file $book. File = ”/home/foobar/books. txt”; use CGI; $query = new CGI; $title = $query->param(’title’); $publisher = $query->(’publ’); … if (open(FILE, ”>>”. $book. File)) { print FILE ”<book subject=””. $subject. ””>n”; print FILE ” <title><![CDATA[”. $title. ”]]></title>n; … print FILE ”</book>nn”;

Notes: n Only an XML fragment is generated: no declaration, no root element <book

Notes: n 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> <num. Pages>279</num. Pages> <sale. Details><isbn>0553293362</isbn> <price>5. 59</price ></sale. Details> <description><![CDATA[After the First Foundation…]]> </description> </book>

n n Some other portion of the library’s application periodically reads the XML data

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

supply. Books. pl $book. File = ”/home/foobar/books. txt” open(FILE, $book. FILE) || die ”Could’t

supply. Books. pl $book. File = ”/home/foobar/books. txt” open(FILE, $book. FILE) || die ”Could’t open $book. File. ”; print ”Content-type: text/plainnn”; print ”<? xml version=” 1. 0”? >n”; print ”<books>n”; while (<FILE>) { print ”$_”; } print ”</books>n”; close(FILE);

mytechbooks. com n n n can make HTTP requests (supply. Books. pl) and receives

mytechbooks. com n n n can make HTTP requests (supply. Books. 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 n n n only computer-related books are to be included

Filtering the XML data n n n 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”

computer. Books. xsl <xsl: stylesheet xmlns: ”http: //…. ” version=” 1. 0”> <xsl: template

computer. Books. 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:

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” /> Publisher: <xsl: value-of select=”publisher” /> Pages: <xsl: value-of select=”num. Pages” /> Price: <xsl: value-of select=”sale. Details/price” /> <xsl: element name=”a”> <xsl: attribute name=”href”> /servlets/Buy. Book. Servlet? isbn= <xsl: value-of select=”sale. Details/isbn” /> </xsl: attribute></xsl: element></td></tr></table> </xsl: template>

XSLT from a servlet import. . . public class List. Books. Servlet extends Http.

XSLT from a servlet import. . . public class List. Books. Servlet extends Http. Servlet { private static final String hostname=”foobar. com”; private static final int port. Number = 80; private static final String file =”/cgi/supply. Books. pl”; private static final String stylesheet = ”/home/…/computer. Books. xsl”; }} public void service(Http. Servlet. Request req, Http. Servlet. Response res) throws … { res. set. Content. Type(”text/html”); URL get. Books. URL = new URL(”http”, hostname, port. Number, file); Input. Stream in = get. Books. URL. open. Stream(); // transform XML into HTML

Notes: n n Servlet requests the Foobar Public Library’s application (supply. Books. pl) through

Notes: n n Servlet requests the Foobar Public Library’s application (supply. Books. pl) through an HTTP request, and gets the XML response in an Input. Stream 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 n There is current no general API that specifies how XSLT transformations

Invoking transformations n 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 n n Apache Xalan: XSLTProcessor class in the org. apache. xalan. xslt

Invoking transformations n n 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 get. Books. URL = new URL (”http”, hostname, port. Number, file); Input.

… URL get. Books. URL = new URL (”http”, hostname, port. Number, file); Input. Stream in = get. Books. URL. open. Stream(); try { XSLTProcessor processor = XSLTProcessor. Factory. get. Processor(); } } processor. process(new XSLTInput. Source(in), new XSLTInput. Source ( new File. Input. Stream(stylesheet)), new XSLTResult. Target( res. get. Output. Stream())); } catch (Exception e) {…}

Push vs. pull n n Pull: the user writes an URL or follows a

Push vs. pull n n 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 n n n Personalized start pages: e. g. Netscape’s

Personalized start pages and RSS n n n 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 n n title of the channel description of the channel image or

RSS channel n n title of the channel description of the channel image or logo items, each item may contain title, description and hyperlink

… in our example n n n mytechbooks. com would like to create an

… in our example n n n 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 will automatically update RSS channel content

<? xml version=” 1. 0”> <!DOCTYPE rss PUBLIC ”-//Netscape Communications//DTD RSS 0. 91//EN” ”http:

<? 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. new. Instance. com/javaxml/techbooks </link> <description>Your online source for technical material, computers, and computing books!</description> <language>en-us</language> <image>…</image> <item>…</item> </channel> </rss>

<item> <title>Java Servlet Programming</title> <link> http: //mytechbooks. com/buy. xsp? isbn=156592391 X </link> <description> This

<item> <title>Java Servlet Programming</title> <link> http: //mytechbooks. com/buy. xsp? isbn=156592391 X </link> <description> This book is a superb introduction to Java servlets and their various communications mechanisms. </description> </item>

Transformation into RSS n n n an XSLT transformation can transform the book elements

Transformation into RSS n n n an XSLT transformation can transform the book elements to item elements: book -> item title -> title sale. Details/isbn -> link (isbn as param. ) description -> description

Validating the RSS channel n n n http: //my. netscape. com/publish/help/ validate. tmpl there

Validating the RSS channel n n n 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 n n n The channel is published to Netcenter (or other

Registering the channel n n n 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 n n button or link is added to the mytechbooks. com web page:

Use n n 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 n n The same RSS format can (and is) used

Not restricted to Netscape n n The same RSS format can (and is) used in different applications format is simple and generic: suitable for many purposes