ASP
  Home arrow ASP arrow Page 4 - SQL Server 2000, XML and XSL: The Ultimate...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

SQL Server 2000, XML and XSL: The Ultimate Combination
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2002-01-07

    Table of Contents:
  • SQL Server 2000, XML and XSL: The Ultimate Combination
  • Creating our sample database
  • Creating the stored procedure
  • Displaying the XML with ASP and XSL
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    <!-- METADATA Type="TypeLib" File="c:\program files\common

    files\system\ado\msado15.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. 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:

    <?xml version="1.0" ?>

    <xsl:stylesheet xmlns:xsl="http://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 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:

    The output from the XSL transformation

    More ASP Articles
    More By Mitchell Harper


     

    ASP ARTICLES

    - Central Scoreboard with Flash and ASP
    - Calorie Counter Using WAP and ASP
    - Creating PGP-Encrypted E-Mails Using ASP
    - Be My Guest in ASP
    - Session Replacement in ASP
    - Securing ASP Data Access Credentials Using t...
    - The Not So Ordinary Address Book
    - Adding and Displaying Data Easily via ASP an...
    - Sending Email From a Form in ASP
    - Adding Member Services in ASP
    - Removing Unconfirmed Members
    - Trapping HTTP 500.100 - Internal Server Error
    - So Many Rows, So Little Time! - Case Study
    - XDO: An XML Engine Class for Classic ASP
    - Credit Card Fraud Prevention Using ASP and C...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway