- Introduction
- The Framework
- The Solution
- Browser Checking
- Static XML Files
- Summary
The Solution
The core component of the solution (the complete source is available here) is the OutputXMLResponse subroutine shown in Listing 1, which checks for the presence of the XML cookie with the UseXMLOutput function, outputting an XML document if the cookie is set, or the transformed HTML markup if the cookie is not present. The parameters to this function are an XML document parsed in a DOM tree (XDoc) and the relative path to the XSLT style sheet (StyleURL).
Listing 1 Main output function.
Sub OutputXMLResponse(XDoc,StyleURL) Dim XSLT,HText If UseXMLOutput Or IsNull(StyleURL) Then OutputXMLDocument XDoc,StyleURL Else HText = LocalXSLTransform(XDoc,StyleURL) OutputUTFtext HText, "text/html" End If End Sub
The UseXMLOutput function, shown in Listing 2, checks for the presence of the XML cookie. Due to incomplete support of XSLT in Internet Explorer 5, it always returns False if the user agent is IE5.
Listing 2 Select XML or HTML response.
Function UseXMLOutput UseXMLOutput = False If InStr(Request ("HTTP_USER_AGENT"),"MSIE 5") > 0 Then Exit Function UseXMLOutput = Request.Cookies("XML") = "1" End Function
The OutputXMLDocument subroutine in Listing 3 is conceptually simple: It prepends the xml-stylesheet processing directive in front of the XML root element, and outputs the value of the xml property of the DOM document by using the OutputUTFtext function. A minor adjustment is needed to ensure that the <?xml ?> processing instruction is always present in the output XML stream; see the source code for the complete subroutine.
Listing 3 Send XML text to the client.
Sub OutputXMLDocument(XDoc,StyleURL) Dim XPI If StyleURL <> "" Then Set XPI = XDoc.createProcessingInstruction("xml-stylesheet", "href=’" & StyleURL & "’ type=’text/xsl’") XDoc.insertBefore XPI, XDoc.firstChild.nextSibling End If OutputUTFtext XDoc.xml,"text/xml" End Sub
When the XML cookie is not set, the LocalXSLTransform function in Listing 4 performs the XSLT transformation. It creates a new DOM document, loads the XSL style sheet into it, and uses the style sheet to transform the input DOM document. You could also enhance its performance with server-cached XSLT processors.
Listing 4 Perform local XSLT transformation.
Function LocalXSLTransform(XDoc,StyleURL) Dim XSLT Set XSLT = Server.CreateObject(DOMClass) SetXMLOptions XDoc : SetXMLOptions XSLT If Not XSLT.Load (Server.MapPath(StyleURL)) Then RaiseError "XSL stylesheet load failed " & StyleURL If XSLT.parseError.errorCode <> 0 Then RaiseError "XML parsing failed: " & XSLT.parseError.reason LocalXSLTransform = XDoc.transformNode(XSLT) End Function Sub SetXMLOptions (XD) XD.Async = False XD.setProperty "ServerHTTPRequest",true XD.setProperty "AllowDocumentFunction",true End Sub Sub RaiseError(Txt) Err.Raise vbObjectError+1, "XML library",Txt End Sub
Finally, the OutputUTFtext function in Listing 5 returns the XML text string or HTML markup to the client. This function clears the output buffer (to erase any previous debugging messages or HTML markup embedded in the script), sets the content-type header, changes the codepage property to a UTF-8 code page, displays the input result text, and stops response processing.
Listing 5 Output UTF-8 text to the client.
Sub OutputUTFtext (txt,contentType) Response.Clear Response.ContentType = contentType Response.Charset = "utf-8" Response.Codepage = 65001 Response.Write txt Response.End End Sub