whump dot com

Compiling Style Sheets Using ASP

global.asa

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

' Error Handler
' From Michael Kay's _XSLT Programmer's Reference_
Sub ShowError(objDoc)
 	' create and display error message
	Dim strError
    
	strError =  "<pre>Invalid XML file!<br />" _
		&  "File URL:      " & objDoc.parseError.url & "<br />" _
		&  "Line No.       " & objDoc.parseError.line & "<br />" _
		&  "Character:     " & objDoc.parseError.linepos & "<br />" _
		&  "File Position: " & objDoc.parseError.filepos & "<br />" _
		&  "Source Text:   " & objDoc.parseError.srcText & "<br />" _
		&  "Error Code:    " & objDoc.parseError.errorCode & "<br />" _
		&  "Description:   " & objDoc.parseError.reason & "</pre>"
		Response.Write strError
End Sub

'Runs once when the web server shuts down
Sub Application_OnEnd
	Set Application = Nothing
End Sub

'Runs once when the first page of your application is run for the first time by any user
'Create the globaly accessible XSLT transform DOMs we need

Sub Application_OnStart   

	Dim headlineXSL
	Dim headline
	Dim prlistXSL
	Dim prlist
	
	Set headlineXSL		= _
		server.CreateObject("Msxml2.FreeThreadedDOMDocument.3.0")
	headlineXSL.async	= false
	headlineXSL.ValidateOnParse = true
	headlineXSL.load Server.MapPath("/include/news.xsl")
	If headlineXSL.parseError.errorCode <> 0 Then
		' error found so show the error message and stop
		ShowError headlineXSL
	End If

	Set Application("headlineTransform") = headlineXSL

	Set prlistXSL		= _
		server.CreateObject("Msxml2.FreeThreadedDOMDocument.3.0")
	prlistXSL.async		= false
	prlistXSL.ValidateOnParse = true
	prlistXSL.load Server.MapPath("/include/newsTease.xsl")
	If prlistXSL.parseError.errorCode <> 0 Then
		' error found so show the error message and stop
		ShowError prlistXSL
	End If	

	Set Application("prlistTransform") = prlistXSL

End Sub

</SCRIPT>

The Application_OnStart routine loads the style sheets and puts them in the Application object where they are accessible by other active server pages. Also notice we're using the free threaded version of the MSXML DOM because this style sheet will be servicing multiple and simultaneous requests.

iXSLtransform.asp

<%
' Before calling/including this code
' you must set strXMLfile and strTransform.
 
' Error Handler

...

' get me an XML DOM
Set objXML = Server.CreateObject("MSXML2.DOMDocument.3.0")
objXML.ValidateOnParse = True

' load the source XML document and check for errors
objXML.load Server.MapPath(strXMLfile)
If objXML.parseError.errorCode <> 0 Then
	' error found so show the error message and stop
	ShowError objXML
End If

' use strTransform as the index into the Application object
' where we stored the compiled style sheets
Set transform = Application(strTransform)
strResult = objXML.transformNode(transform)
Response.Write strResult

Set transform = nothing

%>

On the active server pages where we want to apply a transform:

' set the XML input and the style sheet 
Set strXMLfile = '/include/news.xml'
Set strTransform = 'headlineTransform'

Then include iXSLtransform.asp.


Last modified on 10/1/00; 11:34:10 PM.
Contact WHUMP dot COM.
Copyright © 1995-2000, Bill Humphries