How To Use Active Server Pages to Dynamically Generate XML


This article describes how to use Active Server Pages (ASP) to dynamically create eXtensible Markup Language (XML). The example uses an XSL style sheet to format the results.

NOTE: The sample only works in XML-enabled browsers such as Microsoft Internet Explorer 5 or later.

NOTE: The sample should be considered a starting point in learning and understanding how to output XML from ASP.

More Information

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

ASP/XML Example

This article is divided into the following sections:

A System Data Source Name (DSN) provides a way for ASP pages and other data clients to interface with a database.

NOTE: You have to be an administrator to create a System DSN.

  1. Create a new folder named WebData within the folder that contains the content for your default Web site. This is the <drive>:\InetPub\WWWRoot folder by default.
  2. Copy the NorthWind sample database (Nwind.mdb) to this new folder. To install the NorthWind sample database, install the samples that come with Microsoft Visual Studio 6.0 and Microsoft Office. Visual Studio 6.0 Setup installs Nwind.mdb in <drive>:\Program Files\Microsoft Visual Studio\VB98 by default.
  3. On Microsoft Windows 2000, click Start, point to Programs, point to Administrator Tools, and then click Open Data Sources (ODBC).

    On Microsoft Windows 95, Microsoft Windows 98, and Microsoft Windows NT, open Data Sources (ODBC) in Control Panel.
  4. On the System DSN tab, click Add.
  5. Click Microsoft Access Driver (*.mdb), and then click Finish.
  6. Click Select. In the WebData folder, click the NorthWind database, and then click OK.
  7. In the Data Source Name text box, type NORTHWIND, and then click OK.
  8. Click OK to exit Data Sources (ODBC).

To better view the XML data, create an XSL style sheet to format the data.

  1. In Notepad, paste the following XSL code:
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="uri:xsl">
    <xsl:template match="/">
    <TABLE BORDER="1">
    <xsl:for-each select="//RECORD">
    <xsl:for-each select="FIELD">
    <TD><xsl:value-of select="VALUE"/></TD>
  2. Save the page as Xmltest.xsl in the WebData folder.

Because ASP can format data from a database in many ways, you can output XML from data that is found in the database.

  1. In Notepad, paste the following XSL code:
    <% @ Language="VBScript" %>
    ' Declare all variables.
    Option Explicit
    Dim objCN,objRS,objField
    Dim strSQL,strCN
    Dim strName,strValue

    ' Buffer and output as XML.
    Response.Buffer = True
    Response.ContentType = "text/xml"

    ' Start our XML document.
    Response.Write "<?xml version=""1.0""?>" & vbCrLf
    Response.Write "<?xml:stylesheet type=""text/xsl"" href=""xmltest.xsl""?>" & vbCrLf

    ' Set SQL and database connection string.
    strSQL = "SELECT * FROM Customers"

    ' Open the database connection and recordset.
    Set objCN = Server.CreateObject("ADODB.Connection")
    objCN.Open strCN
    Set objRS = objCN.Execute(strSQL)

    ' Output start of data.
    Response.Write "<DATABASE>" & vbCrLf

    ' Loop through the data records.
    While Not objRS.EOF
    ' Output start of record.
    Response.Write "<RECORD>" & vbCrLf
    ' Loop through the fields in each record.
    For Each objField in objRS.Fields
    strName = objField.Name
    strValue = objField.Value
    If Len(strName) > 0 Then strName = Server.HTMLEncode(strName)
    If Len(strValue) > 0 Then strValue = Server.HTMLEncode(strValue)
    Response.Write "<FIELD>" & vbCrLf
    Response.Write "<NAME>" & strName & "</NAME>" & vbCrLf
    Response.Write "<VALUE>" & strValue & "</VALUE>" & vbCrLf
    Response.Write "</FIELD>" & vbCrLf
    ' Move to next record in database.
    ' Output end of record.
    Response.Write "</RECORD>" & vbCrLf

    ' Output end of data.
    Response.Write "</DATABASE>" & vbCrLf
  2. Save the page as Xmltest.asp in the WebData folder.

In Internet Explorer, type http://localhost/WebData/xmltest.asp in the address bar. An HTML table with the information from the Customers table in the sample database appears. However, if you view the source code to the page, you see only the XML data that the ASP page generated. This occurs because Internet Explorer loads the XML data and then retrieves the XSL style sheet to format the output that actually appears.

In addition, both the XML data and the XSL style sheet are generic in design to allow easy data customization. For example, you could modify the SQL text as follows:
  strSQL = "SELECT * FROM Employees"
This changes the database table that is used, and the code sample still functions properly.


For more information on XML and XSL, see the following World Wide Web Consortium (W3C) Web site:
See also the XML and XSL online documentation on the Microsoft Developer Network (MSDN):
For additional information about how to use client-side data binding to generate XML, click the article number below to view the article in the Microsoft Knowledge Base:

258295 How To Bind to XML Data with Internet Explorer
For additional information about XML and XSL, click the article numbers below to view the articles in the Microsoft Knowledge Base:

264665 How To Display HTML in XSL Style Sheet
272270 How To Retrieve XML Data in ASP with the XML OLE-DB Simple Provider

Artikelnummer: 281099 – Letzte Überarbeitung: 14.08.2008 – Revision: 1