Displaying the Results:
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 the 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:
<!-- METADATA Type="TypeLib" File="c:program filescommon
filessystemadomsado15.dll" -->
<%
dim objStream
dim objConn
dim objComm
dim objXML
set objStream = Server.CreateObject("ADODB.Stream")
set objConn = Server.CreateObject("ADODB.Connection")
set objComm = Server.CreateObject("ADODB.Command")
set objXML = Server.CreateObject("MSXML2.DOMDocument")
objConn.Open "Provider=SQLOLEDB; Data Source=(local); Initial
Catalog=myProducts; UId=sa; Pwd="
objComm.ActiveConnection = objConn
objComm.CommandType = adCmdStoredProc
objComm.CommandText = "sp_GetExplicitXML"
objStream.Open
objComm.Properties("Output Stream").Value = objStream
objComm.Execute ,, adExecuteStream
objStream.Position = 0
objXML.LoadXML("<?xml version='1.0'?><?xml-stylesheet type='text/xsl'
href='prodtest.xsl'?><My_Products>" &
objStream.ReadText & "</My_Products>")
if objXML.parseError.errorCode <> 0 then
Response.Write "Error loading XML: " & objXML.parseError.reason
Response.End
end if
Response.ContentType = "text/xml"
Response.Write objXML.xml
%>
I won’t go into too much detail about the code for our prodtest.asp page. Put simply, 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 output to text/xml and write the 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:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="https://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<title> Sample Products </title>
</head>
<body>
<h1>Books in Catalog</h1>
<table border="0" cellspacing="2" cellpadding="3">
<xsl:for-each select="My_Products/Category">
<tr>
<td width="100%" bgcolor="#C0FFC0">
<xsl:value-of select="@CatName"/>
</td>
</tr>
<xsl:for-each select="Product">
<tr>
<td width="100%" bgcolor="#E9E9E9">
<p style="margin-left:30"><xsl:value-of select="@ProdName"/></p>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Once we’ve saved the stylesheet 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). There results are shown below:
Conclusion:
Microsoft have successfully implemented XML support into their overall Web strategy, and this is blatantly obvious in SQL Server 2000. If some of the code above was a bit hard to understand, persevere. It took me almost two weeks to learn when I first started!
The stored procedure we created is just one of the many ways to incorporate XML into the business logic layer of any n-Tier application, keeping in mind that many, if not all of the world’s top development companies have already incorporated/are planning to incorporate XML support into their products now or in the future. Try these links to broaden your knowledge of XML in SQL Server 2000:
Mitchell is the co-founder and product manager at BigCommerce—SaaS ecommerce software which is used by thousands of businesses to sell online. He can be reached via email at mitch@bigcommerce.com