As I mentioned at the end of my last column, on IIS-based systems, Microsoft's XSLISAPI filter lets you transform XML documents into HTML pages by applying Extensible Style Language (XSL) formatting. More importantly, this transformation occurs entirely on the server and enables a browser to process XML documents regardless of its direct browsing capability. All XML documents are translated to HTML via XSL on their way to the client, which is completely unaware of the process or the XML origin of a document. The client's browser simply navigates to an XML URL and receives something whose MIME type is plain old HTML.
XSLISAPI is an executable that you can download from the MSDN Online Downloads site. Note that XSLISAPI currently works on Windows 2000 Server or Win2K Advanced Server if you install MSXML 2.5 or higher. XSLISAPI won't work on a Win2K Professional box. XSLISAPI works on Windows NT 4.0 Server but with some limitations that the companion documentation describes fully. (Although XSLISAPI is a released product, it might change significantly to fit into the upcoming ASP+ and .NET architecture.)
XSLISAPI chooses different stylesheets based on a client's browser. It also adds some advanced characteristics, such as stylesheet caching for better server performance, support for character encoding, and a customizable structure for error messages. After you download and properly expand xslisapi.exe (it's a self-extracting executable), you need to take some further steps to complete the installation.
First, use the following command-line statement to register the filter:
Then, in the IIS administration Control Panel applet, right-click your Default Web Site node, and select Properties. Select the ISAPI Filters tab and add the new filter to the list. When you close and reopen the Properties dialog box, you should see a green arrow next to the new filter name on the ISAPI Filters tab.
To complete setup, create a new virtual directory— Xslisapi. Point this directory to the folder where you expanded xslisapi.exe. Set the Run Script permissions on this virtual directory.
To associate an XSL file with an XML document, use either the server-config attribute or the href attribute in an XML processing instruction:
<?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" server-config="sampleA-Config.xml" href="sampleA-IE5.xsl" ?>
If you specify both server-config and href (as I have in the example above), the latter takes precedence.
The server-config attribute points to an XML file in the same folder as the XML document. The XML file contains information about which XSL stylesheet to use for a given browser. (In much the same way that ASP pages process browscap.ini, XSLISAPI processes server-config attributes.) Below is a code snippet from a possible server-config file:
<server-styles-config> <device browser="IE" version="5.0"> <stylesheet href="IE5.xsl"/> </device> <device browser="Netscape" version="4.5"> <stylesheet href="NN45.xsl"/> </device> </server-styles-config>
From the server-config file, the filter determines which XSL stylesheet to use and loads it. Next, the filter transforms the XML code into HTML, as illustrated in the following pseudocode:
origPath = Request.ServerVariables("HTTP_SSXSLSRCFILE:"); ServDoc.URL = origPath; ServDoc.UserAgent = Request.ServerVariables("HTTP_USER_AGENT:"); requestPath = Server.MapPath(origPath); ServDoc.Load(requestPath); ServDoc.Transform(Response);
Notice the use of an internal server variable that contains the name of the XSL file to use. The ServDoc object is an instance of XSLISAPI.XMLServerDocument— one of the objects registered by xslisapi2.dll. Because of a bug in all versions of MSXML earlier than version 3.0 (currently in beta, see the MSDN Online XML Developer Center to download the beta), you can't load an XML document into the MSXML runtime using a full URL. Thus, in the example above, I use Server.MapPath to load the XML document. When the XSLISAPI filter is up and running on your Web server, any XML file that a browser points to results in HTML code with all the transformation logic buried in a specified XSL file on the server.
As a final note, consider that the XSLISAPI tool supports multiple stylesheets. If you have multiple <stylesheet> nodes, the system processes them sequentially with the result of one node becoming the input for the next one.