Active Server - Using XML Templates


The last subject we'd like to cover in this article is that of XML template files. These provide us with several key benefits. First, they allow us to significantly shorten the URL we send through the browser by encapsulating all aspects of the query into an external file. Second, they permit us to hide some of the details of the database structure in an external file, so that they aren't as readily visible to the casual visitor to a Web site utilizing these techniques. Third, they provide for much greater flexibility to call parameterized stored procedures, carry out highly complex multi-table queries, and more. All of these can be accomplished directly from the URL, providing that we don't go over its length limit; however, external XML template files are easier to read. Finally, XML template files are reusable. That is rather than copying and pasting the same query into many different places (and dealing with the maintenance headache of changing them all in the event of a necessary modification), we have a single, centralized XML file that can be accessed from many different points. And finally, using ASP, we can even modify these XML template files on the fly should we wish to. We won't demonstrate that technique in this article, but it's something you can experiment with on your own.


So how do we create an XML template file? It's really pretty simple. Let's take the same query we executed with our XSL stylesheet above and convert it into an XML template. As you recall, the URL-based query was as follows:


http://LocalHost/Northwind?sql=SELECT+FirstName,LastName,City+FROM+Employees+ORDER+BY+LastName
+FOR+XML+AUTO&Root=EmployeeTable&xsl=template/Employees.xls


Now examine Listing B, which contains the XML template file equivalent for this query. As you can see, this is much more readable that the URL equivalent. You can feel free to break lines wherever you like and you don't have to escape all of the spaces as the URL form required you to do. You'll also notice that we've specified our XSL stylesheet as an attribute of the root EmployeeTable element.


There are several other optional elements you can include within an XML template, which is beyond the scope of this article. For our purposes, all you need to do is put any standard SQL query inside the <sql:query> element just as you would in an ASP page. Then, to invoke this template from the browser, use a URL of the following form:


http://LocalHost/Northwind/Template/Employees.xml?ContentType=text/html


Note the addition of the ContentType parameter. This is necessary because when you use an XML template, the SQL ISAPI extension in IIS assumes that you want XML returned to the browser. By specifying a ContentType of "text/html," you'll get exactly the same display results as we did with the much longer URL-based query.


Listing B: An XML template file for our query


<EmployeeTable xmlns:sql="urn:schemas-microsoft-com:xml-sql"

    sql:xsl="Employees.xsl"

    <sql:query>

         SELECT FirstName, LastName, City

         FROM Employees

         ORDER BY LastName

         FOR XML AUTO

    </sql:query>

</EmployeeTable


Your Web server just got Wise!


As we alluded, there are many other applications of the techniques we've introduced in this article, including invoking stored procedures, utilizing XPath queries, using ASP to rewrite XML template files on an ad hoc basis, and more elaborate XML template files. These are outside our focus today, but experiment with accessing your SQL Server 2000 databases using the ubiquitous HTTP protocol, and remember that this is just the beginning. In a future article, we'll explore some of these topics in more detail with more advanced examples in ASP, XML, and XSL.

Go back