XML Shopping Cart

Description: This sample demonstrates a simple shopping cart application. It allows a user to query the SQL Server Pubs sample database for a set of books. The application displays a tabular list of books that match her query, and presents a user interface that allows her to make purchase selections. The current set of books that the user wishes to purchase is placed inside of a shopping cart, the contents of which are also displayed in the applications user interface. The user can issue as many queries against the database as she wishes, and can add any number of books to her shopping cart. When she has completed her shopping trip, she can proceed to the checkout, where the shopping cart application will calculate her purchase totals on the server.

More Details

This sample demonstrates several interesting uses of XML. The first is as the means to encode hierarchical data streams. Most traditional RDBMS systems return tabular data sets. Unfortunately, tabular data sets are not the most efficient way to represent information that is acquired as the result of a one-to-many or a many-to-many SQL JOIN operation. XML, by its very nature, is very amenable towards the encoding of hierarchical data streams. In this sample, we will demonstrate the use of the hierarchical recordset feature of ADO 2.1, using the Microsoft Data Shape OLE DB provider. The second interesting use of XML is to dynamically create an XML document on the fly on the client. The dynamically created XML document is used to store the contents of the users shopping cart on the client. We ship the shopping cart XML document to the server at check out time.

This application is an example of a design pattern called a one-page-web application (for details see URL to MIND Article in September 1999). It renders the user interface on the fly on the client, and requests / transmits data to the server side components using XML-encoded HTTP requests. The one-page-web application consists of several components. The sample1.htm file implements the user interface. The runQueryJ.asp file is a server-side component that takes a clients XML-encoded query request and uses it to create a hierarchical ADO recordset. This recordset is then converted on the fly into XML, and the XML-encoded recordset is transmitted back to the client code via HTTP. The checkout.asp file is another server-side component that takes the contents of the XML-encoded shopping cart, and adds up the total of all of the books contained in the shopping cart, and returns the total as an XML-encoded reply to the client code.

The sample1.htm file contains a pair of

elements that constitute the bulk of the user interface. One of the
elements is used to display the contents of the shopping cart and the other is used to display the contents of the current database query. The generateCartHTML() and generateBookListHTML() functions are used to walk through an XML parser object and generate the appropriate HTML for display. The code within these functions could easily be replaced with an Extensible Stylesheet Language-Transformation (XSLT) template, thereby reducing the number of lines of code that have to be written (and tested!) in the application.

The btnAddToCart_onclick() function demonstrates copying nodes from one XML parser object to another. When the user decides to add a selection of books to her shopping cart, this method walks through all of the elements that have been checked on the list, and adds them to the shopping cart XML parser object.

The btnCheckout_onclick() function creates an instance of the XML HTTP Request object, and uses it to POST the XML-encoded shopping cart to the checkout.asp server-side function. This function simply calculates the total of all of the books in the users selection, and returns a simple XML-encoded document that contains the total price of all of the users selections in her shopping cart.

The runQueryJ.asp file contains the code that generates a hierarchical recordset. It takes as its input the XML-encoded request from the client. The request contains three parameters: the computer name of the SQL Server database, the database to use, and a string that represents the WHERE clause of the SQL query. The SQL query takes into consideration that there is a many-to-many relationship between books and book authors. It generates a hierarchical recordset that contains a list of books, and for each book the list of authors for that book. It then converts that list of books to an XML-encoded data stream and transmits that data back to the client.

Note that this sample must be manually copied to your web server. You can find the files that you need to copy in the ADOXML2\ADOXML2_Local subdirectory. To copy the source files to the web server, run the setup.cmd script in the ADOXML2 subdirectory. You must have the Windows Scripting Host installed to run this setup script.

Browser/Platform Compatibility

This sample requires Internet Explorer 5.0 on the client, and Internet Information Server 4.0 on the server. The Microsoft Data Access Components SDK Version 2.1 must be installed on the server.

Note: This sample also requires the RequestExLib.RequestEx COM object, which can be found in the RequestExLib.dll. You can find a copy of this DLL under the Tools\bin directory. It needs to be registered first (run regsvr32 on the file). This COM object is used to ensure that we can read code from an HTTP POST request into an XML Parser object instance using either IIS 4.0 or IIS 5.0.

If you are running Windows 2000, the ASP scripts on the server will conditionally execute a different code path to take advantage of new features that are only available in the Microsoft Data Access Components SDK Version 2.5 and Internet Information Server 5.0.

Complete Article

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish