(c) 2002 Visual Studio Magazine
Fawcette Technical Publications

Issue: December 2002
Section: Create a Template-Based Web Site
Author: Jonny Anderson

ASP, MSXML	Let ASP Control the Flow
Listing 1	The ASP file handles the manipulation of the MSXML objects required to transform XML into HTML using XSLT. Depending on the value of the URL-encoded variable cx (context), the ASP generates a Web page using a combination of XSLTemplate, XSLProcessor, and XMLDom objects. Send the HTML output directly to the client browser by setting the XSLProcessor's HTML output to the Response object.

<%@ Language=VBScript %>
<%
response.contentType="text/html"
set xmlDom= Server.CreateObject _
	("msxml2.FreeThreadedDOMDocument.3.0")
xmlFile=Server.MapPath("esales.xml")
xmlDom.async=False
xmlDom.Load xmlFile
pageAction cint(request("cx"))

sub pageAction (context)
	select case context
		case 0
			outputPage "products.xslt"
		case 1
			outputPage "order.xslt"
		case 2
			createNewOrder
		case 3
			outputPage "status.xslt"
		end select
end sub

sub outputPage(xsltFile)
set xslDom= Server.CreateObject _
	("msxml2.FreeThreadedDOMDocument.3.0")
xslDom.async=False
xslDom.Load Server.MapPath(xsltFile)
set xslTemplate= server.createObject _
	("msxml2.XSLTemplate.3.0")
set xslTemplate.stylesheet=xslDom
set xslProcessor= xslTemplate.createProcessor()
xslProcessor.input=xmlDom
xslParam=cint(request("key"))
if not xslParam = 0 then _
	xslProcessor.addParameter "key", xslParam
xslProcessor.output=Response
xslProcessor.transform
end sub

ASP, MSXML	Create a New Order and Dispatch an Automatic E-mail
Listing 2	A new <order> node is saved to the XML file when users confirm their product selection. This code assigns each new <order> node a series of attributes, including a unique order_key taken from the parent <orders> node's next_key attribute. You maintain this key by incrementing the next_key attribute. You also use the new order_key to create a URL that's included in the body of the e-mail that's dispatched automatically to the user. 

sub createNewOrder()
name=request.form("customer_name")
email=request.form("customer_email")
set orders = xmlDom.selectSingleNode("//orders")
order_key=orders.getAttribute("next_key")
set newOrder = xmlDom.createElement("order")
with newOrder
	.setAttribute "key", order_key
	.setAttribute "name", name
	.setAttribute "email", email
	.setAttribute "product_key", request("key")
	.setAttribute "status", "confirmed on " & _
		formatDateTime(now, vbShortDate)
end with
orders.appendChild newOrder
orders.setAttribute "next_key", order_key+1
xmlDom.save xmlFile
dispatchEmail name, email, order_key
end sub

sub dispatchEmail(name, email, order_key)
set newMail = server.createObject _
	("cdonts.newMail")
msg="Dear " & name &  vbCrLf & vbCrLf 
msg=msg & "To view the status of your " & _
	"order please click on the link below." & _
	vbCrLf & vbCrLf 
msg=msg & "http://www.mycompany.net/" & _
	"esales/esales.asp?cx=3&key=" & order_key
newMail.send "jdoe@test.com", email, _
	"'Not For Sale' order status", msg, 1
response.write "<p>Thank you <b>" & name & _
	"</b>. Your order has been confirmed and an _
	email dispatched to:<b> " & email & _
	"</b>.</p>"
end sub
%>
