SQL Server 2000, XML and XSL: The Ultimate Combination
All the hype that once surrounded XML is finally starting to die down, and developers are really beginning to harness the power and flexibility of the language. In this article, Mitchell will show us how SQL Server 2000 incorporates XML support using the "FOR XML EXPLICIT" method to retrieve data from a table as a customised XML document.
SQL Server 2000, XML and XSL: The Ultimate Combination - Displaying the XML with ASP and XSL (Page 4 of 5 )
Now that we've created our stored procedure named "sp_GetExplicitXML", we'll want to be able to do something useful with it. We can start by making sure the stored procedure returns the results we expected by typing the following code into a new query analyser window:
use myProducts
go
exec sp_GetExplicitXML
If all goes well, you'll be presented with one row of XML that contains the results of the stored procedure. To display our results in a browser, we'll create an ASP script that will execute the stored procedure, load the results into an MSXML DomDocument object, and then transform those results using an XSL stylesheet. Create a new ASP script and call it prodtest.asp. Enter the following code into prodtest.asp and save it into a directory that can be processed by IIS:
I won't go into too much detail about the code for our prodtest.asp page. Basically, we're using ADO command and stream objects to execute our stored procedure and read the results to an MSXML DOMDocument, which is then parsed and checked for errors. If there are no errors, we change the content-type of our browsers output to text/xml and write the XSL transformed XML to the browser.
Notice that a stylesheet is set in our XML document to render the results into an easily readable format. The stylesheet, "prodtest.xsl" is shown below:
Once we've saved the style sheet in the same directory as our "prodtest.asp" file, we can fire up our browser and run "prodtest.asp" (make sure you save the files into a directory that can be processed by IIS). The results are shown below: