Download SQL Server 2000_ado_xsl

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Relational algebra wikipedia , lookup

DBase wikipedia , lookup

Database wikipedia , lookup

Tandem Computers wikipedia , lookup

Microsoft Access wikipedia , lookup

Ingres (database) wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Team Foundation Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Transcript
SQL Server 2000, XML and XSL: The Ultimate Combination
(Page 1 of 5 )
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.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. XML is a data descriptive language that uses a set of userdefined tags to describe data in a hierarchically structured format.
The release of Microsoft SQL Server 2000 saw Microsoft jump on the XML band-wagon too:
they've included a number of different ways to manipulate data as well-formed XML. Firstly,
there's the SQL XML support. Microsoft's implementation of SQL XML provides a simple
configuration tool that allows developers to gain remote access to databases using URL based
queries over HTTP. For example, we can setup an SQL XML virtual directory on our Web server
named "myVirtual". Then, assuming we have the appropriate security permissions, we can use
any browser to query our database using a simple URL based query (such as:
http://www.myserver.com/myVirtual?SQL=select+*+from+products+for+xml+auto). This then
returns our results as a valid XML document.
Notice the "for xml auto" part of our query above? This determines the way in which SQL Server
2000 "shapes" our data. There are three shaping methods:



"for xml auto": Returns XML elements that are nested, based on which tables are
listed in the "from" part of the query and which fields are listed in the "select" part.
"for xml raw": Returns XML elements with the "row" prefix (ex: "<row tProduct ...>").
Each column in a table is represented as an attribute and null column values aren't
included.
"for xml explicit": Explicit mode is the most complex shaping method used in SQL
Server 2000. It allows users to query a data source in such a way that the names and
values of the returned XML are specified before the query batch is executed.
It's the third method, "for xml explicit", that I will discuss today. The explicit method, in my
opinion, is the most powerful feature of SQL Server 2000. Not only can we specify how XML
data is returned to us, but we can also use record filters and sorting patterns as well, because,
as we all know, sorting an XML document any other way is almost impossible.
Now, let's get into it. This article is aimed at the intermediate to advanced developer looking to
use XML in the BLL (business logic layer) of an n-Tier based application where speed is a critical
issue. To benefit from this article, you'll need to equip yourself with the following:


A Win2k box running IIS and SQL Server 2000 with the MSXML 3 (or above) library
installed
Intermediate ASP, SQL, XML and XSL knowledge
SQL Server 2000, XML and XSL: The Ultimate Combination - Creating our sample
database
(Page 2 of 5 )
On your SQL Server, open Enterprise Manager and create a new database named "myProducts".
Then, using either Enterprise Manager or Query Analyzer, create the tables shown below:
(Note: catId, productId and descId are all auto-incrementing identity fields)
As you've probably guessed, we're using three tables to simulate a very simple product
description database (let's assume we sell books). The diagram above shows the hierarchy of
our data: categories listing products, listing their descriptions. Before we progress to the next
step, we'll need to create some dummy data in our tables. To maximize productivity and
minimize the length of this article, I've created a simple T-SQL script, which will populate our
tables as needed. You can download it as part of the support material at the end of this article.
The script will create 3 categories, 7 products and 7 descriptions.
Next: Creating the stored procedure >>
SQL Server 2000, XML and XSL: The Ultimate Combination - Creating the stored
procedure
(Page 3 of 5 )
To retrieve our data as XML, we will compress our query batch into a single stored procedure.
This encourages code reuse strategies and is easily modifiable in the future. Lets start by
loading up query analyzer on our database server (Start -> Programs -> Microsoft SQL Server > Query Analyzer).
When prompted, enter your database login credentials. You should be connected to the server
on which you created the "myProducts" database. Next, enter the following T-SQL commands,
which I will explain shortly:
use myProducts
go
CREATE PROCEDURE sp_GetExplicitXML
AS
SELECT 1 AS Tag,
NULL AS Parent,
c.catName as [Category!1!CatName],
NULL as [Product!2!ProdName],
NULL as [Product!2!Description]
FROM categories c
UNION ALL
SELECT 2 AS Tag,
1 AS Parent,
c.catName,
p.productName,
d.descText
FROM categories c, products p, descriptions d
WHERE c.catId = p.productCat AND p.productId = d.descProdId
ORDER BY [Category!1!CatName], [Product!2!ProdName]
FOR XML EXPLICIT
Don't get scared away by the code for the stored procedure above, I promise, it's easy! Allow
me to explain the code step by step.
use myProducts
go
CREATE PROCEDURE sp_GetExplicitXML
AS
If you've ever worked with T-SQL then you should be familiar with these commands. The "use
myProducts" command tells SQL Server to "run any queries that we execute against the
myProducts database". The "go" command tells SQL server to execute all the code up until that
line. Next, we tell SQL server that we will create a new stored procedure named
"sp_GetExplicitXML". The stored procedure will accept no input parameters and will not return
any output values.
SELECT 1 AS Tag,
NULL AS Parent,
c.catName as [Category!1!CatName],
NULL as [Product!2!ProdName],
NULL as [Product!2!Description]
FROM categories c
UNION ALL
SELECT 2 AS Tag,
1 AS Parent,
c.catName,
p.productName,
d.descText
FROM categories c, products p, descriptions d
This is the main chunk of code for our stored procedure. Remember how I said earlier, that
when using the "FOR XML EXPLICIT" mode, you could control the shape, column names and
content of the returned XML? Well, this is the code that does that for us. The code above acts as
a template into which a universal table will be created. A universal table is similar to a normally
mapped SQL table with just a couple of differences:



A universal table mimics a hierarchical structure by using "tag" and "parent" fields to
measure the depth of the data hierarchy. You'll notice in our code above, that the first
select command has a tag of 1 with no parent. In the next select statement, there is a
tag of 2, with a parent of 1, which means that all the results returned from the second
select statement will be children of the first select statement.
As you've probably noticed, the column names for a universal table differ from normal
column names. Lets break down the name of one of our universal table column names
from the code above:
[Category!1!CatName] As you can see, the column name has three values, each
separated by an exclamation mark. The first value is the name of the XML element that
the universal table will produce. The second is the tag index, which lets SQL's internal
XML pointer know which level of the hierarchy into which this data will be placed. The
last value, "CatName" is the name of the attribute into which the value of the select
statement will be inserted. A sample category element from our database would look
like this:
<Category CatName="ASP">
Notice how there are two NULL values in the first select statement? That's because in the first
statement, we're only concerned with the categories for our books, which come from the
categories table ("FROM categories c"). These values are left NULL because they'll be filled in as
part of the next select statement (where we deal with the categories, products and descriptions
tables).
Now, to the last part of the code:
WHERE c.catId = p.productCat AND p.productId = d.descProdId
ORDER BY [Category!1!CatName], [Product!2!ProdName]
FOR XML EXPLICIT
In this final chunk of our code, we make sure that each category displays only the products
whose "productCat" field is equal to their "catId" field. Also, we match each product with its
description using the "p.productId = d.descProdId" equality test. Lastly, we sort the XML output
by ascending category name, and ascending product name (NB: Remember to run the code by
pressing Alt+X).
Next: Displaying the XML with ASP and XSL >>
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:
Next: Conclusion >>
SQL Server 2000, XML and XSL: The Ultimate Combination - Conclusion
(Page 5 of 5 )
Microsoft has 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. Try the related links and books below to broaden your knowledge of XML in
SQL Server 2000.